Amusing D facts: typesafe variadic arrays are lazy!

downs default_357-line at yahoo.de
Tue Oct 13 10:19:31 PDT 2009


downs wrote:
> Did you know the following code compiles?
> 
>> module test;
>>
>> import std.stdio;
>>
>> void Assert(bool cond, string delegate()[] dgs...) {
>>   debug if (!cond) {
>>     string str;
>>     foreach (dg; dgs) str ~= dg();
>>     throw new Exception(str);
>>   }
>> }
>>
>> void main() {
>>   Assert(false, "O hai thar! ");
>> }
> 
> 
> It's true! :)

Here is a funny consequence of this amusing fact:

if you overload opAssign in a struct with lazy T[] dgs..., you can achieve the following syntax


>     WithFlag(GL_BLEND, true) = WithDepthMask(false) = tex.With = Quads = {
>       foreach (i, q; qa) {
>         float f = 1f*i / qa.length; Color(1f-f, f, 1f);
>         TexCoord(0f, 0f); Vertex(q.points[0]);
>         TexCoord(1f, 0f); Vertex(q.points[1]);
>         TexCoord(1f, 1f); Vertex(q.points[3]);
>         TexCoord(0f, 1f); Vertex(q.points[2]);
>       }
>     };



More information about the Digitalmars-d mailing list