Re: How do I properly exit from a D program (outside main)?

2014-09-16 Thread Jacob Carlborg via Digitalmars-d-learn

On 16/09/14 02:06, AsmMan wrote:


Neither assert or return will help. Check out this code example:

void main() {
f();
}

void f() {
   if(!foo)
 exit(1);
do_something();
}


If you want to exit the application with exit code 1 then it sounds like 
something has gone wrong. In that case, throw an exception instead.


--
/Jacob Carlborg


Re: Segfault when casting array of Interface types

2014-09-16 Thread Klaus via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 03:05:57 UTC, Franz wrote:

On Tuesday, 16 September 2014 at 03:03:16 UTC, Franz wrote:

On Tuesday, 16 September 2014 at 02:21:42 UTC, rcor wrote:
I'm back for another round of is this a bug, or am I doing 
something stupid?.


C and D implement interface I, and I have an array of each. 
I'd like to combine these into one I[], but eventually I'd 
like to cast an element back to its original type.


interface I {}
class C : I {}
class D : I {}

void main() {
C[] c = [new C, new C];
D[] d = [new D, new D];
auto i = cast(I[]) c ~ cast(I[]) d;
assert(cast(C) i[0]); // segfault
}

casting each array to I[] is fine, but casting an element 
back to C segfaults (even if it weren't a C, it should just 
return null, right?)


look at what 's auto has created:

import std.stdio;

interface I {}
class C : I {}
class D : I {}

void main() {
 C[] c = [new C, new C];
 D[] d = [new D, new D];
 auto i = cast(I[]) c ~ cast(I[]) d;
 writeln(typeof(i).stringof);
}

Your issue comme from auto.


i is a I[]


writing

   auto i = cast(I[]) c ~ cast(I[]) d;

is just a horrible way of shortcuting the static typing. You 
write this thinking that i has to be... and then you complain 
latter because the cast does not work.
D is a strongly typed lang. in your example you use auto 
because your brain doesnt give you what the type of i has to be, 
which is an error. D is not a scripting lang. You made a wrong 
usage of auto.




Docs for PR

2014-09-16 Thread Ilya Yaroshenko via Digitalmars-d-learn

How generate documentation for phobos PR?


Re: Segfault when casting array of Interface types

2014-09-16 Thread via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 06:27:59 UTC, Klaus wrote:

On Tuesday, 16 September 2014 at 03:05:57 UTC, Franz wrote:

On Tuesday, 16 September 2014 at 03:03:16 UTC, Franz wrote:

On Tuesday, 16 September 2014 at 02:21:42 UTC, rcor wrote:
I'm back for another round of is this a bug, or am I doing 
something stupid?.


C and D implement interface I, and I have an array of each. 
I'd like to combine these into one I[], but eventually I'd 
like to cast an element back to its original type.


interface I {}
class C : I {}
class D : I {}

void main() {
C[] c = [new C, new C];
D[] d = [new D, new D];
auto i = cast(I[]) c ~ cast(I[]) d;
assert(cast(C) i[0]); // segfault
}

casting each array to I[] is fine, but casting an element 
back to C segfaults (even if it weren't a C, it should just 
return null, right?)


look at what 's auto has created:

import std.stdio;

interface I {}
class C : I {}
class D : I {}

void main() {
C[] c = [new C, new C];
D[] d = [new D, new D];
auto i = cast(I[]) c ~ cast(I[]) d;
writeln(typeof(i).stringof);
}

Your issue comme from auto.


i is a I[]


writing

   auto i = cast(I[]) c ~ cast(I[]) d;

is just a horrible way of shortcuting the static typing. You 
write this thinking that i has to be... and then you complain 
latter because the cast does not work.
D is a strongly typed lang. in your example you use auto 
because your brain doesnt give you what the type of i has to 
be, which is an error. D is not a scripting lang. You made a 
wrong usage of auto.


AFACIS there's nothing wrong with his use of casting. It's fine 
here, because `I` is a base type of `C` and `D`. If it weren't 
for the arrays, the cast wouldn't even be necessary. I think it's 
a bug.


Re: Segfault when casting array of Interface types

2014-09-16 Thread via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 08:39:43 UTC, Marc Schütz wrote:
AFACIS there's nothing wrong with his use of casting. It's fine 
here, because `I` is a base type of `C` and `D`. If it weren't 
for the arrays, the cast wouldn't even be necessary. I think 
it's a bug.


Correction:

AFAIK casting between interfaces and classes needs to adjust the 
underlying pointer. Therefore, when casting an array, the 
compiler would have to do that with the entire array, which 
cannot be copied without allocating memory (and mustn't be 
modified in-place for consistency reasons). This means that the 
cast is instead a pure reinterpret cast (repainting).


Whether the compiler should accept that or not is a different 
question. I guess it should, because if it doesn't, there 
wouldn't be an easy way to achieve a reinterpret cast (only via 
an intermediate cast to `void*`, which is clumsy).


Anyway, using `std.conv.to` is the way to go here (if you don't 
require that last bit of performance), because it is safer in 
general (also checks for overflows and the like, for example).


Re: Function Pointers with Type T

2014-09-16 Thread evilrat via Digitalmars-d-learn
On Tuesday, 16 September 2014 at 01:16:14 UTC, Adam D. Ruppe 
wrote:


bool function(T val1,T val2) ptr=comp!T;


Moreover, comp has compile time arguments, so you can't take 
the address of it without forwarding the arguments. So instead 
of comp, you use comp!T - passing the T from the outside to 
the comparison function too.


i wish i knew that back when i needed it, so i could avoid 
spaghetti mess and not abandon my code :(


Re: Segfault when casting array of Interface types

2014-09-16 Thread rcor via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 08:49:04 UTC, Marc Schütz wrote:
On Tuesday, 16 September 2014 at 08:39:43 UTC, Marc Schütz 
wrote:




Whether the compiler should accept that or not is a different 
question. I guess it should, because if it doesn't, there 
wouldn't be an easy way to achieve a reinterpret cast (only via 
an intermediate cast to `void*`, which is clumsy).


Anyway, using `std.conv.to` is the way to go here (if you don't 
require that last bit of performance), because it is safer in 
general (also checks for overflows and the like, for example).


Thanks, didn't think of trying std.conv.to. Can someone expand a 
bit on what to! is doing in this situation that cast isn't? I 
looked up 'reinterpret cast' but didn't see the connection to 
this.




AFAIK casting between interfaces and classes needs to adjust 
the underlying pointer. Therefore, when casting an array, the 
compiler would have to do that with the entire array, which 
cannot be copied without allocating memory (and mustn't be 
modified in-place for consistency reasons). This means that the 
cast is instead a pure reinterpret cast (repainting).


Is to! creating a new array of pointers while cast isn't? This 
isn't a performance critical section and it's not a huge array, 
so I ask mostly out of curiosity.


Re: Docs for PR

2014-09-16 Thread Robert burner Schadek via Digitalmars-d-learn
On Tuesday, 16 September 2014 at 08:26:57 UTC, Ilya Yaroshenko 
wrote:

How generate documentation for phobos PR?


http://wiki.dlang.org/Building_DMD has a section called building 
the docs. I properly missed something while writing that, but it 
is good starting point and the rest you will find in the 
makefile. that is only tested on *nix


Re: Segfault when casting array of Interface types

2014-09-16 Thread rcor via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 06:27:59 UTC, Klaus wrote:


is just a horrible way of shortcuting the static typing. You 
write this thinking that i has to be... and then you complain 
latter because the cast does not work.
D is a strongly typed lang. in your example you use auto 
because your brain doesnt give you what the type of i has to 
be, which is an error. D is not a scripting lang. You made a 
wrong usage of auto.


Admittedly this came about as a result of some poor design on my 
part, but I don't get what you're saying about auto. I thought 
auto was supposed to relieve the cognitive load of manual type 
identification. Without it, std.algorithm would be a pain to use. 
My brain doesn't intuitively tell me that std.algorithm.filter 
returns a FilterResult, but I can use it effectively with auto.


Why if(__ctfe)?

2014-09-16 Thread Ilya Yaroshenko via Digitalmars-d-learn

Why not static if(__ctfe) ?


Re: Why if(__ctfe)?

2014-09-16 Thread Adam D. Ruppe via Digitalmars-d-learn
On Tuesday, 16 September 2014 at 13:11:50 UTC, Ilya Yaroshenko 
wrote:

Why not static if(__ctfe) ?


ctfe is a runtime condition. The function has the same code when 
run at compile time, it is just being run in a different 
environment.


Re: Segfault when casting array of Interface types

2014-09-16 Thread via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 11:26:05 UTC, rcor wrote:
On Tuesday, 16 September 2014 at 08:49:04 UTC, Marc Schütz 
wrote:
On Tuesday, 16 September 2014 at 08:39:43 UTC, Marc Schütz 
wrote:




Whether the compiler should accept that or not is a different 
question. I guess it should, because if it doesn't, there 
wouldn't be an easy way to achieve a reinterpret cast (only 
via an intermediate cast to `void*`, which is clumsy).


Anyway, using `std.conv.to` is the way to go here (if you 
don't require that last bit of performance), because it is 
safer in general (also checks for overflows and the like, for 
example).


Thanks, didn't think of trying std.conv.to. Can someone expand 
a bit on what to! is doing in this situation that cast isn't? I 
looked up 'reinterpret cast' but didn't see the connection to 
this.


Reinterpret cast means that the compiler should treat whatever is 
at the memory location as the given type, and not modify it in 
any way. `std.conv.to` can do more work, for example it can also 
parse strings into integers:


assert(42.to!int == 42);





AFAIK casting between interfaces and classes needs to adjust 
the underlying pointer. Therefore, when casting an array, the 
compiler would have to do that with the entire array, which 
cannot be copied without allocating memory (and mustn't be 
modified in-place for consistency reasons). This means that 
the cast is instead a pure reinterpret cast (repainting).


Is to! creating a new array of pointers while cast isn't? This 
isn't a performance critical section and it's not a huge array, 
so I ask mostly out of curiosity.


Yes, it is. (Probably. I don't have the time to test it now, but 
it's likely if my theory about the pointer adjustment is correct.)


Re: Why if(__ctfe)?

2014-09-16 Thread Ilya Yaroshenko via Digitalmars-d-learn
On Tuesday, 16 September 2014 at 13:28:17 UTC, Rene Zwanenburg 
wrote:
On Tuesday, 16 September 2014 at 13:17:28 UTC, Adam D. Ruppe 
wrote:
On Tuesday, 16 September 2014 at 13:11:50 UTC, Ilya Yaroshenko 
wrote:

Why not static if(__ctfe) ?


ctfe is a runtime condition. The function has the same code 
when run at compile time, it is just being run in a different 
environment.


Note that if(__ctfe) does not incur a runtime performance 
penalty. Even in debug builds will the branch be removed.


It is a kind of magic ;)


Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Jay via Digitalmars-d-learn
all the functions/methods i've come across so far deal with 
either streams or just file names (like std.file.read) and there 
doesn't seem to be a way to wrap a std.stdio.File in a stream (or 
is there?). i need a function that takes a std.stdio.File and 
returns a string or byte array.


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 14:37:06 UTC, Jay wrote:
all the functions/methods i've come across so far deal with 
either streams or just file names (like std.file.read) and 
there doesn't seem to be a way to wrap a std.stdio.File in a 
stream (or is there?). i need a function that takes a 
std.stdio.File and returns a string or byte array.


You could try `std.mmfile.MmFile`, it has a constructor that 
takes a `File`, but only on Linux:


http://dlang.org/phobos/std_mmfile.html
https://github.com/D-Programming-Language/phobos/blob/master/std/mmfile.d#L77-L79


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Daniel Kozak via Digitalmars-d-learn
V Tue, 16 Sep 2014 14:37:05 +
Jay via Digitalmars-d-learn digitalmars-d-learn@puremagic.com napsáno:

 all the functions/methods i've come across so far deal with 
 either streams or just file names (like std.file.read) and there 
 doesn't seem to be a way to wrap a std.stdio.File in a stream (or 
 is there?). i need a function that takes a std.stdio.File and 
 returns a string or byte array.


You can use rawRead:
http://dlang.org/phobos/std_stdio.html#.File.rawRead



Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Jay via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 14:43:10 UTC, Marc Schütz wrote:
You could try `std.mmfile.MmFile`, it has a constructor that 
takes a `File`, but only on Linux:


http://dlang.org/phobos/std_mmfile.html
https://github.com/D-Programming-Language/phobos/blob/master/std/mmfile.d#L77-L79


does it work with pipes/sockets? it seems to call fstat() to get 
the size of the contents. but anyway this is quite useful, thanks.


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Jay via Digitalmars-d-learn
On Tuesday, 16 September 2014 at 14:49:08 UTC, Daniel Kozak via 
Digitalmars-d-learn wrote:

You can use rawRead:
http://dlang.org/phobos/std_stdio.html#.File.rawRead


for that i need to know the size of the contents. is there a 
function that does that for me? i'm looking for something like 
someFile.readAll(). and it shouldn't matter whether the File 
instance represents a regular file or a pipe/stream.


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Jay via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 15:03:05 UTC, Jay wrote:
and it shouldn't matter whether the File instance represents a 
regular file or a pipe/stream.


i meant pipe/socket.


switch statement exiting a void function

2014-09-16 Thread Jonathan via Digitalmars-d-learn

Here's the setup, I have a function

void main { ... }

The main method parses input (via std.getopt) and calls one of 
three void-return-type functions.  The program's three options 
correspond to significantly different initialization options.


In the code we then have:

enum RunOpt {opt1, opt2, opt3};

And the body of the function wants to do:

RunOpt option;
//parsing that results in, among other things option being 
initialized

switch(option){
case RunOpt.opt1: fun1(...);
case RunOpt.opt2: fun2(...);
default: fun3(...);
}

When compiling, the error I get is

Error: switch case fallthrough - use 'goto case;' if intended

This is not intended.  Note that calling return; after 
funi(...) makes everything work.  However, it feels like I'm 
doing something wrong here?


Re: switch statement exiting a void function

2014-09-16 Thread bearophile via Digitalmars-d-learn

Jonathan:

This is not intended.  Note that calling return; after 
funi(...) makes everything work.  However, it feels like I'm 
doing something wrong here?


Try:

enum RunOpt { opt1, opt2, opt3 } // No semicolon here

final switch (option) with (RunOpt) {
case opt1: fun1(...); break;
case opt2: fun2(...); break;
case opt3: fun3(...); break;
}

Bye,
bearophile


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 14:59:45 UTC, Jay wrote:
On Tuesday, 16 September 2014 at 14:43:10 UTC, Marc Schütz 
wrote:
You could try `std.mmfile.MmFile`, it has a constructor that 
takes a `File`, but only on Linux:


http://dlang.org/phobos/std_mmfile.html
https://github.com/D-Programming-Language/phobos/blob/master/std/mmfile.d#L77-L79


does it work with pipes/sockets? it seems to call fstat() to 
get the size of the contents. but anyway this is quite useful, 
thanks.


mmap(2) says it fails with EACCES when the fd isn't a regular 
file, so unfortunately it won't work.


sort using delegate as predicate

2014-09-16 Thread via Digitalmars-d-learn
The following code does not compile, because the custom predicate 
of std.algorithm.sort is a template parameter, and therefore can 
only be a function, but not a delegate. In C++, there is a 
variant of sort taking a function-object as a second (run-time) 
parameter, but phobos does not seems to have such a thing. It 
seems like a pretty common problem to me, so does anyone have a 
solution?




class Foo
{
  int[] order;

  bool cmp(int a, int b) const
  {
return order[a]  order[b];
  }
}

void main()
{
  auto f = new Foo;
  int[] data;
  sort!(f.cmp)(data);
}


Re: sort using delegate as predicate

2014-09-16 Thread Justin Whear via Digitalmars-d-learn
On Tue, 16 Sep 2014 16:19:10 +, Simon Bürger wrote:

 The following code does not compile, because the custom predicate of
 std.algorithm.sort is a template parameter, and therefore can only be a
 function, but not a delegate. In C++, there is a variant of sort taking
 a function-object as a second (run-time) parameter, but phobos does not
 seems to have such a thing. It seems like a pretty common problem to me,
 so does anyone have a solution?

Bit of a workaround, but this should do it:

   sort!((a,b) = f.cmp(a, b))(data);


Re: sort using delegate as predicate

2014-09-16 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Sep 16, 2014 at 04:19:10PM +, via Digitalmars-d-learn wrote:
 The following code does not compile, because the custom predicate of
 std.algorithm.sort is a template parameter, and therefore can only be
 a function, but not a delegate.

What makes you think so? Template parameters certainly can be delegates.
I use that all the time.


 In C++, there is a variant of sort taking a function-object as a
 second (run-time) parameter, but phobos does not seems to have such a
 thing. It seems like a pretty common problem to me, so does anyone
 have a solution?
 
 
 
 class Foo
 {
   int[] order;
 
   bool cmp(int a, int b) const
   {
 return order[a]  order[b];
   }
 }
 
 void main()
 {
   auto f = new Foo;
   int[] data;
   sort!(f.cmp)(data);
 }

Try this:

sort!(f.cmp)(data);

In D, writing `f.cmp` (without the ) can in some contexts be
misinterpreted as calling the function and passing the return value to
the template parameter, which is probably why you're getting a compile
error.


T

-- 
Why ask rhetorical questions? -- JC


Re: sort using delegate as predicate

2014-09-16 Thread via Digitalmars-d-learn
On Tuesday, 16 September 2014 at 16:27:46 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
On Tue, Sep 16, 2014 at 04:19:10PM +, via 
Digitalmars-d-learn wrote:
The following code does not compile, because the custom 
predicate of
std.algorithm.sort is a template parameter, and therefore can 
only be

a function, but not a delegate.


What makes you think so? Template parameters certainly can be 
delegates.

I use that all the time.


Well because the delegate contains a pointer to the object, which 
can not be known at compile time. In fact



sort!(f.cmp)(data);


fails with the error variable f cannot be read at compile time, 
which seems reasonable to me. On the other hand



sort!((a,b) = f.cmp(a, b))(data);


does in fact compile, so i guess problem is solved. Thanks guys.


Re: How to pass a member function/delegate into a mixin template?

2014-09-16 Thread 岩倉 澪

On Monday, 15 September 2014 at 21:37:50 UTC, John Colvin wrote:
would this work for you? alias is the usual way of taking a 
function as a template parameter.


mixin template EventListener(alias slot)


Ah, thanks for the help! That works great. :)



Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Ali Çehreli via Digitalmars-d-learn

On 09/16/2014 07:37 AM, Jay wrote:

all the functions/methods i've come across so far deal with either
streams or just file names (like std.file.read) and there doesn't seem
to be a way to wrap a std.stdio.File in a stream (or is there?). i need
a function that takes a std.stdio.File and returns a string or byte array.


std.file.read (and readText):

  http://dlang.org/phobos/std_file.html#.read

Ali



Re: sort using delegate as predicate

2014-09-16 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Sep 16, 2014 at 04:38:04PM +, via Digitalmars-d-learn wrote:
 On Tuesday, 16 September 2014 at 16:27:46 UTC, H. S. Teoh via
 Digitalmars-d-learn wrote:
 On Tue, Sep 16, 2014 at 04:19:10PM +, via Digitalmars-d-learn wrote:
 The following code does not compile, because the custom predicate of
 std.algorithm.sort is a template parameter, and therefore can only
 be a function, but not a delegate.
 
 What makes you think so? Template parameters certainly can be
 delegates.  I use that all the time.
 
 Well because the delegate contains a pointer to the object, which can
 not be known at compile time. In fact
 
 sort!(f.cmp)(data);
 
 fails with the error variable f cannot be read at compile time,
 which seems reasonable to me. On the other hand
 
 sort!((a,b) = f.cmp(a, b))(data);
 
 does in fact compile, so i guess problem is solved. Thanks guys.

Mea culpa, I confused lambdas with delegates. The two are not the same
thing. That will teach me to answer emails while rushing to get ready
for work. :-P Sorry for the noise.


T

-- 
There is no gravity. The earth sucks.


Re: dub can't read files from cache

2014-09-16 Thread Ruslan via Digitalmars-d-learn
Note. ╨а╤Г╤Б╨╗╨░╨╜ is a cyrillic word. That should not affect 
because dub only displays so.




Re: switch statement exiting a void function

2014-09-16 Thread Jonathan via Digitalmars-d-learn

Try:

enum RunOpt { opt1, opt2, opt3 } // No semicolon here

final switch (option) with (RunOpt) {
case opt1: fun1(...); break;
case opt2: fun2(...); break;
case opt3: fun3(...); break;
}

Bye,
bearophile


My hero.



Re: Function Pointers with Type T

2014-09-16 Thread amparacha via Digitalmars-d-learn

Thanks Adam you saved me from alot.Just one more question how can
I compare two arguments of type T.
On Tuesday, 16 September 2014 at 01:16:14 UTC, Adam D. Ruppe
wrote:

You can get the code to compile with two changes:

bool function(T)(T val1,T val2) ptr=comp;

should be:

bool function(T val1,T val2) ptr=comp!T;



The function pointer itself isn't a template, so it doesn't 
need the (T) parameter. Instead, since it is inside a template, 
you can just use the T from the outside directly.


Moreover, comp has compile time arguments, so you can't take 
the address of it without forwarding the arguments. So instead 
of comp, you use comp!T - passing the T from the outside to 
the comparison function too.


and also

int partition(T)(T[]list,bool function(T)(T val1,T val2)ptr,int

should be:

int partition(T)(T[]list,bool function(T val1,T val2)ptr,int


Again because the pointer isn't a new template, it should just 
use the type T from the outer argument list.


Re: Function Pointers with Type T

2014-09-16 Thread amparacha via Digitalmars-d-learn

Thanks its the right help at right time.
On Tuesday, 16 September 2014 at 09:13:38 UTC, evilrat wrote:
On Tuesday, 16 September 2014 at 01:16:14 UTC, Adam D. Ruppe 
wrote:


bool function(T val1,T val2) ptr=comp!T;


Moreover, comp has compile time arguments, so you can't take 
the address of it without forwarding the arguments. So instead 
of comp, you use comp!T - passing the T from the outside to 
the comparison function too.


i wish i knew that back when i needed it, so i could avoid 
spaghetti mess and not abandon my code :(


Re: Function Pointers with Type T

2014-09-16 Thread amparacha via Digitalmars-d-learn

Thankx
On Tuesday, 16 September 2014 at 01:21:57 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Tue, 16 Sep 2014 01:09:27 +
amparacha via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

first change:
  bool function(T)(T val1,T val2) ptr=comp;
to
  auto ptr = comp!T;

second change:
  int partition(T)(T[]list,bool function(T)(T val1,T 
val2)ptr,int left,int right)}

to
  int partition(T)(T[]list,bool function(T val1,T val2)ptr,int 
left,int right){




Re: Function Pointers with Type T

2014-09-16 Thread Adam D. Ruppe via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 17:32:02 UTC, amparacha wrote:
Thanks Adam you saved me from alot.Just one more question how 
can

I compare two arguments of type T.


If you want to compare the values, just use them like regular 
variables. If you want to compare the types, use:


static if(is(T == R)) { }

or one of the other forms here 
http://dlang.org/expression.html#IsExpression


For example:

bool typesMatch(T, R)() {
   static if(is(T == R)) return true;
   return false;
}

writeln(typesMatch!(int, float)); // false
writeln(typesMatch!(int, int)); // true


Re: Segfault when casting array of Interface types

2014-09-16 Thread rcor via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 14:13:48 UTC, Marc Schütz wrote:

On Tuesday, 16 September 2014 at 11:26:05 UTC, rcor wrote:


Is to! creating a new array of pointers while cast isn't? This 
isn't a performance critical section and it's not a huge 
array, so I ask mostly out of curiosity.


Yes, it is. (Probably. I don't have the time to test it now, 
but it's likely if my theory about the pointer adjustment is 
correct.)


I guess I could have checked that out myself:

import std.stdio;
import std.conv;

interface I {}
class C : I {}
class D : I {}

void main() {
  C[] c = [new C, new C];
  I[] i = cast(I[]) c;
  I[] i2 = c.to!(I[]);
  assert(c is cast(C[]) i);// i and c point to same address
  assert(i !is i2);// to! appears to create a new 
array

}


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Jay via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 16:54:17 UTC, Ali Çehreli wrote:

On 09/16/2014 07:37 AM, Jay wrote:
all the functions/methods i've come across so far deal with 
either
streams or just file names (like std.file.read) and there 
doesn't seem
to be a way to wrap a std.stdio.File in a stream (or is 
there?). i need
a function that takes a std.stdio.File and returns a string or 
byte array.


std.file.read (and readText):

  http://dlang.org/phobos/std_file.html#.read

Ali


wait, std.file.read doesn't accept a File instance. i've got a 
File instance generated by another function and now i need to 
read the entire contents of whatever it represents (a regular 
file/pipe/etc).


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Justin Whear via Digitalmars-d-learn
On Tue, 16 Sep 2014 14:37:05 +, Jay wrote:

 all the functions/methods i've come across so far deal with either
 streams or just file names (like std.file.read) and there doesn't seem
 to be a way to wrap a std.stdio.File in a stream (or is there?). i need
 a function that takes a std.stdio.File and returns a string or byte
 array.

The short answer is no.  I usually use something like this:

// Lazy
auto stream = stdin.byChunk(4096).joiner();

You can make it eager by tacking a `.array` on the end.  Functions used 
are from std.stdio, std.algorithm, std.array.


compile automation, makefile, or whatever?

2014-09-16 Thread K.K. via Digitalmars-d-learn

Hey I have a quick question: Does D have it's own version of
makefiles or anything (preferably simpler)?
So instead of typing in PowerShell dmd file1.d file2.d
lib\foo.lib -Isrc\ . I could just type most of that into a
file and then just type dmd file.X

I've seen some people make really complex .d files that have a
lot of interchangeability but at the moment I wouldn't really
need something of that scale. Also, I'm not using DUB; I'd prefer
to just use the command line.

..Can pragma's help with this, aside from linking just the libs?


Re: Is there a function that reads the entire contents of a std.stdio.File?

2014-09-16 Thread Jay via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 18:42:42 UTC, Justin Whear wrote:

The short answer is no.  I usually use something like this:

// Lazy
auto stream = stdin.byChunk(4096).joiner();

You can make it eager by tacking a `.array` on the end.  
Functions used

are from std.stdio, std.algorithm, std.array.


thanks. that's exactly what i need.


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 19:00:05 UTC, K.K. wrote:

Hey I have a quick question: Does D have it's own version of
makefiles or anything (preferably simpler)?
So instead of typing in PowerShell dmd file1.d file2.d
lib\foo.lib -Isrc\ . I could just type most of that into a
file and then just type dmd file.X

I've seen some people make really complex .d files that have a
lot of interchangeability but at the moment I wouldn't really
need something of that scale. Also, I'm not using DUB; I'd 
prefer

to just use the command line.

..Can pragma's help with this, aside from linking just the libs?


I want to say somewhere on the forums are some descriptions of
using CMake for this.  Might try searching for that.


Re: core.exception.UnicodeException@src\rt\util\utf.d(400): illegal UTF-16 value

2014-09-16 Thread notna via Digitalmars-d-learn

Thanks Ali.

As always, your examples and explanations are amazingly clear and 
easy to understand.



On Monday, 15 September 2014 at 23:57:59 UTC, Ali Çehreli wrote:
You must make use of the returned value to slice your wstring. 
Something like this (not compiled):


auto actualLength = GetUserNameW(lpwszUsername.ptr, 
dUsername2);

auto userName = lpwszUsername[0..actualLength];

Otherwise, D knows that lpwszUsername is a 254-char string and 
will try to print all of it.


Ali




Re: core.exception.UnicodeException@src\rt\util\utf.d(400): illegal UTF-16 value

2014-09-16 Thread notna via Digitalmars-d-learn

Thanks AsmMan,

Found 
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724432%28v=vs.85%29.aspx 
and with your help, now understand :)


The syntax below prints a blank after my username, so the slice 
seems a bit too long :O So my final solution looks like:


 module main;

 import std.stdio;
 import std.c.windows.windows;

 pragma(lib, user32.lib);
 pragma(lib, Advapi32);

 void main(string[] args)
 {
WCHAR lpwszUsername[254];
DWORD dUsername2 = 254;
GetUserNameW(lpwszUsername.ptr, dUsername2);
writefln(Welcome userW %s, lpwszUsername[0..(dUsername2 - 
1));

writeln();
  }

THANKS again!!!

On Tuesday, 16 September 2014 at 00:03:55 UTC, AsmMan wrote:


GetUserNameW() return a zero on error and non-zero on success. 
The actual number of bytes copied into lpwszUsernam is in 
dUsername2.


auto userName = lpwszUsername[0..dUsername2];




Re: compile automation, makefile, or whatever?

2014-09-16 Thread K.K. via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 19:26:29 UTC, Cliff wrote:

I want to say somewhere on the forums are some descriptions of
using CMake for this.  Might try searching for that.


Yeah I just looked up the CMake thing. It definitely seems worth
playing with, though I'm not really sure how extensive it's D
support currently is :S


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:29:12 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 19:26:29 UTC, Cliff wrote:

I want to say somewhere on the forums are some descriptions of
using CMake for this.  Might try searching for that.


Yeah I just looked up the CMake thing. It definitely seems worth
playing with, though I'm not really sure how extensive it's D
support currently is :S


Out of curiosity, why are you not using dub (on the command-line)?


Re: compile automation, makefile, or whatever?

2014-09-16 Thread K.K. via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:31:33 UTC, Cliff wrote:
Out of curiosity, why are you not using dub (on the 
command-line)?


I'm not against using it or anything, but I've found that it
didn't help me significantly nor did I have the patience to
figure out it's whole set of issues, D by itself is already
enough trouble xD

Plus with my spastic work style, it kinda slowed me down.

However, it is something I may consider when I have an actually
organized project with a final goal. A lot of what I'm doing
right now is just experiments. Though, if Cmake + D does the
trick then I might not use DUB in the end. Hard to say atm.


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:45:29 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 20:31:33 UTC, Cliff wrote:
Out of curiosity, why are you not using dub (on the 
command-line)?


I'm not against using it or anything, but I've found that it
didn't help me significantly nor did I have the patience to
figure out it's whole set of issues, D by itself is already
enough trouble xD

Plus with my spastic work style, it kinda slowed me down.

However, it is something I may consider when I have an actually
organized project with a final goal. A lot of what I'm doing
right now is just experiments. Though, if Cmake + D does the
trick then I might not use DUB in the end. Hard to say atm.


Would you be willing to provide some more detail on what about it
you didn't like (errors, missing features, etc.)?  I ask because
build systems are a particular interest of mine and I have
projects in this area which can always use more user data.


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Andrei Amatuni via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 19:00:05 UTC, K.K. wrote:

Hey I have a quick question: Does D have it's own version of
makefiles or anything (preferably simpler)?
So instead of typing in PowerShell dmd file1.d file2.d
lib\foo.lib -Isrc\ . I could just type most of that into a
file and then just type dmd file.X

I've seen some people make really complex .d files that have a
lot of interchangeability but at the moment I wouldn't really
need something of that scale. Also, I'm not using DUB; I'd 
prefer

to just use the command line.

..Can pragma's help with this, aside from linking just the libs?


Have you tried - http://dlang.org/rdmd.html


Re: compile automation, makefile, or whatever?

2014-09-16 Thread K.K. via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:53:08 UTC, Cliff wrote:
Would you be willing to provide some more detail on what about 
it

you didn't like (errors, missing features, etc.)?  I ask because
build systems are a particular interest of mine and I have
projects in this area which can always use more user data.


I'll try, but I haven't used it at all since maybe.. April?

One of the main things that annoyed me about it was how sensitive
it could be. The best comparison I can give is that it reminded
me ALOT of Haxe. Both are very flimsy, and very poorly
documented. (Nothing beats a good manual as far as I'm concerned!)

The other thing, as I briefly mentioned, was it really didn't
speed anything up, unless maybe you were working on a larger
project.


Obviously I'm not a master of any sort, but the main point I'd
take from this is it wasn't inviting.

Hope that helps a bit :3


Re: compile automation, makefile, or whatever?

2014-09-16 Thread K.K. via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:57:20 UTC, Andrei Amatuni
wrote:

Have you tried - http://dlang.org/rdmd.html


only a tiny bit... I've haven't quite figured rdmd yet :\

I'm actually trying it out right now.


Re: compile automation, makefile, or whatever?

2014-09-16 Thread Cliff via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 21:05:18 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 20:53:08 UTC, Cliff wrote:
Would you be willing to provide some more detail on what about 
it
you didn't like (errors, missing features, etc.)?  I ask 
because

build systems are a particular interest of mine and I have
projects in this area which can always use more user data.


I'll try, but I haven't used it at all since maybe.. April?

One of the main things that annoyed me about it was how 
sensitive

it could be. The best comparison I can give is that it reminded
me ALOT of Haxe. Both are very flimsy, and very poorly
documented. (Nothing beats a good manual as far as I'm 
concerned!)


The other thing, as I briefly mentioned, was it really didn't
speed anything up, unless maybe you were working on a larger
project.


Obviously I'm not a master of any sort, but the main point I'd
take from this is it wasn't inviting.

Hope that helps a bit :3


Yep, that's useful information to me.  Over the years I have
found that build systems *generally* tend to be uninviting.  My
suspicion is that comes down to a few reasons:

1. Builds end up being a LOT more complicated that you would
expect as soon as you step out of a single project with a few
source files and default options
2. Build tooling is typically built and maintained by people who
end up being relatively specialist - either they are part of the
small cabal of people who know the tooling intimately, or they
have been forced into it and know just enough to get by for their
organization.
3. Most build tooling is designed to solve a particular subset of
actual build-related problems, with much less though given to how
it fits holistically into the entire developer workflow.
4. Build tooling is almost never treated like an actual product -
documentation is written for wizards, not lay-people.

As a result, the casual user is a bit SOL.

(NOTE: This is not a rant specifically aimed at DUB, but my
general observation on the state of build tooling.)


Re: compile automation, makefile, or whatever?

2014-09-16 Thread K.K. via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 21:17:19 UTC, Cliff wrote:

On Tuesday, 16 September 2014 at 21:05:18 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 20:53:08 UTC, Cliff wrote:
Would you be willing to provide some more detail on what 
about it
you didn't like (errors, missing features, etc.)?  I ask 
because

build systems are a particular interest of mine and I have
projects in this area which can always use more user data.


I'll try, but I haven't used it at all since maybe.. April?

One of the main things that annoyed me about it was how 
sensitive

it could be. The best comparison I can give is that it reminded
me ALOT of Haxe. Both are very flimsy, and very poorly
documented. (Nothing beats a good manual as far as I'm 
concerned!)


The other thing, as I briefly mentioned, was it really didn't
speed anything up, unless maybe you were working on a larger
project.


Obviously I'm not a master of any sort, but the main point I'd
take from this is it wasn't inviting.

Hope that helps a bit :3


Yep, that's useful information to me.  Over the years I have
found that build systems *generally* tend to be uninviting.  My
suspicion is that comes down to a few reasons:

1. Builds end up being a LOT more complicated that you would
expect as soon as you step out of a single project with a few
source files and default options
2. Build tooling is typically built and maintained by people who
end up being relatively specialist - either they are part of the
small cabal of people who know the tooling intimately, or they
have been forced into it and know just enough to get by for 
their

organization.
3. Most build tooling is designed to solve a particular subset 
of
actual build-related problems, with much less though given to 
how

it fits holistically into the entire developer workflow.
4. Build tooling is almost never treated like an actual product 
-

documentation is written for wizards, not lay-people.

As a result, the casual user is a bit SOL.

(NOTE: This is not a rant specifically aimed at DUB, but my
general observation on the state of build tooling.)


Yeah I absolutely agree with you on that!

Something I see alot in place of actual documentation is the
developer repeatedly saying email me (aka google searching
peoples name's and usernames until you find it), but I'm
generally reluctant to do so because it means I'm gonna have to
ask them very straight forward streamlined questions (usually
meaning I'd have to take my problem out of the context I'm having
trouble with) and then pray that they'll answer more than one
email, and also hope they'll answer within at least a few days.
Some people are really good an answer within a few hours, other
times you won't get a reply at all.

Also when reading your post i remembered something else about
DUB.. I don't remember precisely what I was doing, but I had a
quick experiment that was just supposed a window opened and
displayed a small image. Using DMD it worked fine, but for some
reason when using DUB, the picture wouldn't load at all. It was
quite the mystery.
So you can probably see why I've happily made text files,
folders, and powershell my go to tools :P


Re: compile automation, makefile, or whatever?

2014-09-16 Thread bachmeier via Digitalmars-d-learn

On Tuesday, 16 September 2014 at 20:31:33 UTC, Cliff wrote:

On Tuesday, 16 September 2014 at 20:29:12 UTC, K.K. wrote:

On Tuesday, 16 September 2014 at 19:26:29 UTC, Cliff wrote:

I want to say somewhere on the forums are some descriptions of
using CMake for this.  Might try searching for that.


Yeah I just looked up the CMake thing. It definitely seems 
worth

playing with, though I'm not really sure how extensive it's D
support currently is :S


Out of curiosity, why are you not using dub (on the 
command-line)?


Is dub intended to be a replacement for make? I've never used it 
beyond just playing around with it here and there, but my 
impression was that it is wonderful if you're doing a pure D 
project and have a dependency on some of the libraries in their 
repository. In my case, I'm calling libraries on my local machine 
that I wrote and linking to existing C libraries. I couldn't 
figure out how to do that in dub, but given that the alternative 
was to type a few lines into a Makefile, I didn't see the point.


exporting analysispoint labels into symbol tables

2014-09-16 Thread Jay Norwood via Digitalmars-d-learn
I have a use case that requires  repeating performance 
measurements of blocks of code that do not coincide with function 
start and stop.  For example, a function will be calling several 
sub-operations, and I  need to measure the execution from the 
call statement until the execution of the statement following the 
call.


So, ideally, I'd like to mark the start and stop points in the 
source code with label pairs, and have these exported as 
symbol/address pairs.


I would read these label names with an external app, and would 
set up the performance measurement start and stop window 
boundaries without modifying the target code.


Does D provide any feature that would allow me to export such 
labels?  I've seen some discussion of use of goto labels within 
the program, but nothing about exporting them for use by an 
external app.  I've also read through the recent info on user 
annotations, but those seem to be associated with data properties 
and it isn't apparent to me if they could provide program address 
info.


Thanks,
Jay



Code doesn't work - why?

2014-09-16 Thread Robin via Digitalmars-d-learn

Hello,

I came back to D after a longer break and just wanted to set up a 
small project to further get in contact with this language.


However, it seems that the codes which simply shall simulate a 
deterministic finit automaton do not work correctly.


CODE ---

struct DeterministicState {
public:
	this(string name, bool isFinal, DeterministicState[char] 
transits...) {

this.name = name;
this.finalState = isFinal;
this.addTransits(transits);
}

this(string name, bool isFinal) {
this.name = name;
this.finalState = isFinal;
}

this(bool isFinal, DeterministicState[char] transits...) {
this(, isFinal, transits);
}

this(DeterministicState[char] transits...) {
this(, false, transits);
}

void addTransits(DeterministicState[char] newTransits) {
foreach (immutable key; newTransits.keys) {
transits[key] = newTransits[key];
}
}

string getName() const {
return name;
}

bool isFinalState() const {
return finalState;
}

bool hasNext(char input) const {
return (input in transits) ? true : false;
}

DeterministicState getNext(char input) {
return transits[input];
}

string toString() const {
return name;
}

private:
string name;
DeterministicState[char] transits;
bool finalState;
}

struct DeterministicFiniteAutomaton {
public:
DeterministicState[] input(char[] input) {
DeterministicState[] trace = [ start ];
auto currentState = trace[0];
foreach (immutable c; input) {
if (currentState.hasNext(c) == false) {
writeln(currentState.toString() ~  has next for  ~ 
to!string(c));

break;
} else {
writeln(currentState.toString() ~  has NO next for  ~ 
to!string(c));

}
currentState = currentState.getNext(c);
trace ~= currentState;
}
return trace;
}

this(DeterministicState start) {
this.start = start;
}

private:
DeterministicState start;
}

void main()
{
auto s0 = DeterministicState(s0, false);
auto s1 = DeterministicState(s1, false);
auto s2 = DeterministicState(s2, true);
s0.addTransits(['0' : s1, '1' : s2]);
s1.addTransits(['0' : s0, '1' : s2]);
s2.addTransits(['0' : s2, '1' : s2]);
auto dfa = DeterministicFiniteAutomaton(s0);
auto trace = dfa.input(0001.dup);
foreach (t; trace) {
writeln(t.toString());
}
writeln(Trace Length =  ~ to!string(trace.length));
}

---

The output is the following:

s0 has NO next for 0
s1 has next for 0
s0
s1
Trace Length = 2

Which states that the trace for input 0001 has just a length of 
2 instead of 4. And I do not really understand why s1 has no next 
item while it was defined in main.


I hope someone can clear things up for me. I really don't get why 
this isn't working as intended.


Regards,
Rob


Re: Code doesn't work - why?

2014-09-16 Thread Ali Çehreli via Digitalmars-d-learn

On 09/16/2014 09:08 PM, Robin wrote:

 struct DeterministicState {

Structs are value types. When they are copied, the mutations that you 
expect may be happening on a copy.


I don't understand what exactly should happen but the following changes 
produce at least a different output. :)


1) Change the 'struct' above to 'class':

class DeterministicState {

2) Use the 'override' keyword with toString:

 override string toString() const {

3) Create the objects with new:

auto s0 = new DeterministicState(s0, false);
auto s1 = new DeterministicState(s1, false);
auto s2 = new DeterministicState(s2, true);

Here is the output after that.

s0 has NO next for 0
s1 has NO next for 0
s0 has NO next for 0
s1 has NO next for 1
s0
s1
s0
s1
s2
Trace Length = 5

4) Also, the following conditional seems backward to me:

  if (currentState.hasNext(c) == false) {
  writeln(currentState.toString() ~  has next for  ~
 to!string(c));

Should it not be simply 'if (currentState.hasNext(c))'?

Ali