casting int[] to bool[]

Daniel Keep daniel.keep.lists at gmail.com
Thu Jan 29 14:36:43 PST 2009



Saaa wrote:
> This will take some time to understand.
> Things I have never used before : D
> Tuple, variadic function, template, mixin
> ..stringof(can't find this one), static if
> 
> I'll read up until I understand this.
> 
> One question I can ask.
> Why is that foreach loop run at compile time?
> The compiler checks for a template parameter?
> 
> [snip]

Tuples are like arrays that have a fixed length, where each element can
be of absolutely any type at all.

What this means is that it potentially takes different code to access
each element of a tuple.  So when you see

> foreach( i ; Range!(4) ) foo(i);

What is actually being generated is this:

> foo(0);
> foo(1);
> foo(2);
> foo(3);

It's not really doing the foreach at compile time, but it is unrolling
it.  You might think "but why doesn't this happen for arrays?"  Because
you can't do THIS with arrays:

> foreach( i,x ; Tuple!(0, "b", 3.0) ) writefln("Element %s == %s",i,x);

Which expands to:

> writefln("Element %s == %s",0,0);
> writefln("Element %s == %s",1,"b");
> writefln("Element %s == %s",2,3.0);

 -- Daniel


More information about the Digitalmars-d-learn mailing list