Any ideas for lazy evaluation on varaible argument functions?

Darryl B darrylbleau at gmail.com
Fri Oct 19 20:35:12 PDT 2007


== Quote from Daniel Keep (daniel.keep.lists at gmail.com)'s article
> Aaah.  It's those bloody static arrays again!

Yar, and they be tormenting me code, they is!

> Try this instead of that last line: t.infoF("{}"[], "infoF3"[]);

This does indeed work, and the infoF call this way seems to properly handle the
laziness (not running expensiveFunction() when print is false). This is _almost_
prettier than t.infoF(t.format()), even, though a tad strange to type out. :)

> One of the absolute banes of template metaprogramming in D are
> statically sized arrays.  For instance, the type of "{}" is *not*
> char[]; it's char[2u].

Aha! I see.

> So yeah: you *can* do this, you just need to put [] after any
> statically-sized array variables or array literals.

I also tried just plain old:

infoF(lazy char[] fmt, ...)

But it seems that all the variable _arguments are ran regardless of whether infoF
uses any of them or not. (ie, infoF("{}", expensiveFunction()); runs
expensiveFunction no matter what). It would be nice to be able to do infoF(lazy
char fmt, lazy ...);, or similar. I was also trying something like
infoF(A...)(lazy A a) { foreach(t; a) { this.info(a); } } but that also has the
same effect (expensiveFunction() still evaluated even if this.info doesn't call
the delegate).



More information about the Digitalmars-d mailing list