Re: Read Once then reset/init value?

2019-11-03 Thread Robert M. Münch via Digitalmars-d-learn

On 2019-10-29 23:28:35 +, Simen Kjærås said:


On Tuesday, 29 October 2019 at 22:24:20 UTC, Robert M. Münch wrote:
I quite often have the pattern where a value should be read just once 
and after this reset itself. The idea is to avoid that others read the 
value by accident and get an older state, instead they get an 
"invalid/reset" value.


Is there a library function that can mimic such a behaviour?


Something like this?

T readOnce(T)(ref T value) {
 auto tmp = value;
 value = T.init;
 return tmp;
} unittest {
 int i = 3;
 assert(i.readOnce == 3);
 assert(i == 0);
}

If so, no, there is no library function for it, but feel free to use 
the above. You may very well have to change T.init to something more 
fitting for your use case, of course.


Hi, that looks very good. I forgot about the UFCS possibility. That's 
very good because it works on basic types too. Thanks :-)


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: Mimicking Java's Type Erasure

2019-11-03 Thread Superstar64 via Digitalmars-d-learn

On Monday, 4 November 2019 at 01:25:36 UTC, Heromyth wrote:

On Monday, 4 November 2019 at 00:16:53 UTC, Superstar64 wrote:

Consider the following Java code.
--
import java.util.function.Function;
public class Main{

//basic Rank2 type
static interface Stringer{
 String show(Function type, A that);
}

static class Say implements Stringer {
public  String show(Function type, A 
that){

return type.apply(that);
}
}

static class Shout implements Stringer {
public  String show(Function type, A 
that){

return type.apply(that) + "!!!";
}
}

}
--
This uses Java's generics' type erasure to create a basic rank 
2 type.
What are some clean ways to implement something similar in D, 
in a type safe manner if preferable?


String show(A)(Stringer stringer, Function type, A 
that) {

   Shout shout = cast(Shout)stringer;
   if(shout !is null) {
 shout.show(type, that); return;
   }

   Say say = cast(Say)stringer;
   if(say !is null) {
 say.show(type, that); return;
   }

  assert(false, "Unsupported");
}

Stringer stringer = new Shout();
stringer.show(type, that);

I don't think you understood my question.
Your `shout.show(type,that)` expression wouldn't compile because 
you can't have you can't have a templated virtual function in an 
interface(a rank 2 type).


What I'm asking if there is a way to take a rank 1 type and 
create a delegate to it.


``
string show(A)(string delegate(A) callback, A that){
// ...
}

void main(){
// borrow haskell's syntax for a minute
(forall A. string delegate(string delegate(A),A)) shower = 
show;

show(a => a,"hello").writeln;
}
``
Now I understand this isn't possible natively possible because of 
how D's templates works, but I'm asking if there's a good 
workaround to replicate that behavior.


Re: Mimicking Java's Type Erasure

2019-11-03 Thread Heromyth via Digitalmars-d-learn

On Monday, 4 November 2019 at 00:16:53 UTC, Superstar64 wrote:

Consider the following Java code.
--
import java.util.function.Function;
public class Main{

//basic Rank2 type
static interface Stringer{
 String show(Function type, A that);
}

static class Say implements Stringer {
public  String show(Function type, A that){
return type.apply(that);
}
}

static class Shout implements Stringer {
public  String show(Function type, A that){
return type.apply(that) + "!!!";
}
}

}
--
This uses Java's generics' type erasure to create a basic rank 
2 type.
What are some clean ways to implement something similar in D, 
in a type safe manner if preferable?


String show(A)(Stringer stringer, Function type, A 
that) {

   Shout shout = cast(Shout)stringer;
   if(shout !is null) {
 shout.show(type, that); return;
   }

   Say say = cast(Say)stringer;
   if(say !is null) {
 say.show(type, that); return;
   }

  assert(false, "Unsupported");
}

Stringer stringer = new Shout();
stringer.show(type, that);



Re: Alternative to C++ macro in D

2019-11-03 Thread Ali Çehreli via Digitalmars-d-learn

On 11/03/2019 08:55 AM, Vinod K Chandran wrote:
> Hi all,
> I can do this in C++.
> #include 
> using namespace std ;
>
> #define end };
> #define log(x)  cout << x << endl
> #define wait std::cin.get()

There is nothing that stops one from using the C++ preprocessor on any 
text file. For example, you can do the following wherever GCC exists. If 
there are the following lines in a D file:


#define XYZ 42

auto a = XYZ;

Then you can pass it through the preprocessor like this:

  cpp foo.d

In fact, that's one of the tricks dpp uses to make C++ headers usable in 
D code:


  https://github.com/atilaneves/dpp

As you can see above, compile-time constants are defined with 'enum' in D:

enum XYZ = 42;

auto a = XYZ;

Beware though: Do not do that with arrays though, as every usage of an 
'enum' array causes dynamic memory allocation at run time.


enum myConstants = [ 1, 2 ];

bool foo(int i) {
  import std.algorithm : canFind;
  return myConstants.canFind(i);// <-- DON'T DO THIS
}

In addition to the enum version of an array (which you may need to use 
e.g. with 'static foreach') , also use a run-time initialized array:


// Use this e.g. for 'static foreach'
enum myConstants_enum = [ 1, 2 ];

// Use this for run-time code
immutable int[] myConstants;
shared static this() {
  // This is the initialization of the immutable array:
  myConstants = myConstants_enum;
}

bool foo(int i) {
  import std.algorithm : canFind;
  return myConstants.canFind(i);// <-- DO THIS
}

Even though I've known about this gotcha, this wisdom comes from 
profiling my program by compiling with dmd's '-profile=gc' option. It 
was pretty obvious in the generated 'profilegc.log' file that I was 
causing such unnecessary memory allocations.


Ali



Mimicking Java's Type Erasure

2019-11-03 Thread Superstar64 via Digitalmars-d-learn

Consider the following Java code.
--
import java.util.function.Function;
public class Main{

//basic Rank2 type
static interface Stringer{
 String show(Function type, A that);
}

static class Say implements Stringer {
public  String show(Function type, A that){
return type.apply(that);
}
}

static class Shout implements Stringer {
public  String show(Function type, A that){
return type.apply(that) + "!!!";
}
}

}
--
This uses Java's generics' type erasure to create a basic rank 2 
type.
What are some clean ways to implement something similar in D, in 
a type safe manner if preferable?


Re: Which is the active fork in DFL gui library ?

2019-11-03 Thread Jesse Phillips via Digitalmars-d-learn
On Sunday, 3 November 2019 at 16:48:52 UTC, Vinod K Chandran 
wrote:

On Sunday, 3 November 2019 at 14:01:03 UTC, Jesse Phillips



https://github.com/Rayerd/dfl


@Jesse Phillips,
Thank you for the reply.  Does DWT is built upon Java's SWT ? I 
heard that SWT is somewhat slower in windows. Anyhow, what 
about the easiness of DWT ? Actually, i just want to make GUI 
for Windows only. I dont need a cross platform GUI.


DTW is a translation of swt, I can speak to speed comparisons but 
I don't think you could apply anything out their related to 
comparing dfl and dwt.


You can write windows only apps in dwt, don't compile for Linux, 
it uses native drawing.


Re: Alternative to C++ macro in D

2019-11-03 Thread Meta via Digitalmars-d-learn
On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran 
wrote:

Hi all,
I can do this in C++. #include 
using namespace std ;

#define end };
#define log(x)  cout << x << endl
#define wait std::cin.get()


int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ; mixin template cToD(string code)


`log` and `wait` are straightforward. Just write a function:

import std.stdio;
void log(T)(T x) { writeln(x); }
void wait() { readln(); }

However, you can't do things like `#define end }`. The D language 
intentionally disallows doing stuff like this. If you *really* 
want to do this, you can sort of emulate it with mixins:


mixin template cToD(string code)
{
import std.array: replace;
mixin(code.replace("end", "}"));
}

mixin cToD!`
int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ;
wait ;
return 0;
end
`;

But I would strongly recommend against it.


Re: Alternative to C++ macro in D

2019-11-03 Thread Sebastiaan Koppe via Digitalmars-d-learn
On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran 
wrote:

int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ;
wait ;
end
How can i do this in D ? Especially the " #define end }; ". 
I've tried " alias end = } " but didn't worked.


You can't. D deliberately doesn't have a preprocessor. `#define 
end };` is one of the reasons.


Re: Alternative to C++ macro in D

2019-11-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran 
wrote:

Hi all,
I can do this in C++.
#include 
using namespace std ;

#define end };
#define log(x)  cout << x << endl
#define wait std::cin.get()


int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ;
wait ;
end
How can i do this in D ? Especially the " #define end }; ". 
I've tried " alias end = } " but didn't worked.
Edit : How can add syntax highlighting and coloring in code 
posted on this comment ?


If this is about logging functionality, check 
std.experimental.log package, it contains all necessary logging 
functionality.


About macros there aren't any similar to preprocessor macros in 
c/c++, however you can replace them with three options depending 
on your needs:


1. Just a simple function in conjunction eith ctfe: 
https://tour.dlang.org/tour/en/gems/compile-time-function-evaluation-ctfe

2. string mixins: https://dlang.org/articles/mixin.html
3. template mixins: https://dlang.org/spec/template-mixin.html

I'd say number 2 should be suitable for your example given you'd 
like to inject statements into body of some function.


Best regards,
Alexandru.


Re: Alternative to C++ macro in D

2019-11-03 Thread Alexandru Ermicioi via Digitalmars-d-learn
On Sunday, 3 November 2019 at 17:02:30 UTC, Vinod K Chandran 
wrote:
On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran 
wrote:

Hi all,
I can do this in C++.
#include 
using namespace std ;

#define end };
#define log(x)  cout << x << endl
#define wait std::cin.get()


int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ;
wait ;
end
How can i do this in D ? Especially the " #define end }; ". 
I've tried " alias end = } " but didn't worked.
Edit : How can add syntax highlighting and coloring in code 
posted on this comment ?


How can i edit my post ? There is no button or link to edit my 
post.


You can't, web version is just a frontend for mailing list.

Best regards,
Alexandru.


Re: Alternative to C++ macro in D

2019-11-03 Thread Vinod K Chandran via Digitalmars-d-learn
On Sunday, 3 November 2019 at 16:55:36 UTC, Vinod K Chandran 
wrote:

Hi all,
I can do this in C++.
#include 
using namespace std ;

#define end };
#define log(x)  cout << x << endl
#define wait std::cin.get()


int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ;
wait ;
end
How can i do this in D ? Especially the " #define end }; ". 
I've tried " alias end = } " but didn't worked.
Edit : How can add syntax highlighting and coloring in code 
posted on this comment ?


How can i edit my post ? There is no button or link to edit my 
post.


Alternative to C++ macro in D

2019-11-03 Thread Vinod K Chandran via Digitalmars-d-learn

Hi all,
I can do this in C++.
#include 
using namespace std ;

#define end };
#define log(x)  cout << x << endl
#define wait std::cin.get()


int main() {
log("Trying to avoid the visual clutter aused by closing 
curly braces") ;

string myStr = "Now, code looks more elegant" ;
log(myStr) ;
wait ;
end
How can i do this in D ? Especially the " #define end }; ". I've 
tried " alias end = } " but didn't worked.
Edit : How can add syntax highlighting and coloring in code 
posted on this comment ?


Re: Which is the active fork in DFL gui library ?

2019-11-03 Thread Vinod K Chandran via Digitalmars-d-learn

On Sunday, 3 November 2019 at 07:07:42 UTC, Mike Parker wrote:

On Sunday, 3 November 2019 at 07:06:12 UTC, Mike Parker wrote:



Here's an example, winhello.d, that should work with all of 
the following command lines:




Sorry, here's the example:

== winhello.d

/+ dub.sdl:
name "entry"
dflags "-L/SUBSYSTEM:WINDOWS" "-L/ENTRY:mainCRTStartup" 
platform="windows-dmd"

+/

import core.sys.windows.windows;
pragma(lib, "user32");

void main() {
MessageBoxA(null, "Hello", "Hello", MB_OK);
}


@mike Parker,
Thank you for the detailed and helpful reply. I will sure try it.


Re: Which is the active fork in DFL gui library ?

2019-11-03 Thread Vinod K Chandran via Digitalmars-d-learn

On Sunday, 3 November 2019 at 14:01:03 UTC, Jesse Phillips wrote:
On Saturday, 2 November 2019 at 20:01:27 UTC, Vinod K Chandran 
wrote:

Hi all,
I just found that DFL gui library very interesting. But after 
some searching, i can see that DFL is inactive and there is 
few other forks for it. So this is my question - Which fork is 
good for a gui development in windows platform.
BTW, i just tested the gtkD and successfully compiled a hello 
app. How do i avoid the console window when compiling gtkD app 
?

Thanks in advance.


The last one I used was from rayerd. But even that is behind. I 
switched my app to dwt.


https://github.com/Rayerd/dfl


@Jesse Phillips,
Thank you for the reply.  Does DWT is built upon Java's SWT ? I 
heard that SWT is somewhat slower in windows. Anyhow, what about 
the easiness of DWT ? Actually, i just want to make GUI for 
Windows only. I dont need a cross platform GUI.


Re: Which is the active fork in DFL gui library ?

2019-11-03 Thread Jesse Phillips via Digitalmars-d-learn
On Saturday, 2 November 2019 at 20:01:27 UTC, Vinod K Chandran 
wrote:

Hi all,
I just found that DFL gui library very interesting. But after 
some searching, i can see that DFL is inactive and there is few 
other forks for it. So this is my question - Which fork is good 
for a gui development in windows platform.
BTW, i just tested the gtkD and successfully compiled a hello 
app. How do i avoid the console window when compiling gtkD app ?

Thanks in advance.


The last one I used was from rayerd. But even that is behind. I 
switched my app to dwt.


https://github.com/Rayerd/dfl


Re: Which is the active fork in DFL gui library ?

2019-11-03 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 2 November 2019 at 20:01:27 UTC, Vinod K Chandran 
wrote:

Hi all,
I just found that DFL gui library very interesting. But after 
some searching, i can see that DFL is inactive and there is few 
other forks for it. So this is my question - Which fork is good 
for a gui development in windows platform.


DFL is a long, long dead library. It was created with D1. I'm 
unaware of any active fork.



BTW, i just tested the gtkD and successfully compiled a hello 
app. How do i avoid the console window when compiling gtkD app ?

Thanks in advance.


Any Windows executable compiled with a main function is by 
default considered a "console subsystem" application. You can 
specify to the linker that it should be a "windows subsystem" 
application (for which a console window will not be created) with 
a linker command line switch.


Assuming you're using DMD, when you're using the OPTLINK linker 
(which is the default when invoking DMD directly or when passing 
-m32, or the DUB switch -ax86), the switch is /SUBSYSTEM:WINDOWS. 
You can pass it on the DMD command line with -L, as in:


-L/SUBSYSTEM:WINDOWS

With the Microsoft linker (-m32mscoff or -m64 on the dmd command 
line, -ax86mscoff or -x86_64 with dub, or the default with recent 
64-bit dub versions), it's the same switch. But you also need to 
specify the entry point as being main and not WinMain, so:


-L/SUBSYSTEM:WINDOWS -L/ENTRY:mainCRTStartup

When using dub, you can put the appropriate flags in a "dflags" 
entry in your dub.json or dub.sdl.


Here's an example, winhello.d, that should work with all of the 
following command lines:


dub -ax86 --single winhello.d
dub -ax86_mscoff --single winhello.d
dub -ax86_64 --single winhello.d

dmd -L/SUBSYSTEM:WINDOWS winhello.d
dmd -L/SUBSYSTEM:WINDOWS -L/ENTRY:mainCRTstartup -m32mscoff 
winhello.d

dmd -L/SUBSYSTEM:WINDOWS -L/ENTRY:mainCRTstartup -m64 winhello.d

If you don't have the Microsoft build tools installed, -m32mscoff 
and -m64 will use the lld linker that ships with DMD (if you 
chose to install it when you installed dmd). In that case, I'm 
not sure if the switches are the same. I've never used it and 
don't have it installed.


Re: Which is the active fork in DFL gui library ?

2019-11-03 Thread Mike Parker via Digitalmars-d-learn

On Sunday, 3 November 2019 at 07:06:12 UTC, Mike Parker wrote:



Here's an example, winhello.d, that should work with all of the 
following command lines:




Sorry, here's the example:

== winhello.d

/+ dub.sdl:
name "entry"
dflags "-L/SUBSYSTEM:WINDOWS" "-L/ENTRY:mainCRTStartup" 
platform="windows-dmd"

+/

import core.sys.windows.windows;
pragma(lib, "user32");

void main() {
MessageBoxA(null, "Hello", "Hello", MB_OK);
}