plans for macros

Steven Schveighoffer schveiguy at yahoo.com
Thu May 15 13:29:02 PDT 2008


"Steven Schveighoffer" wrote
> That helps, but the arguments are still evaluated even if the if statement 
> is false.
>
> For example:
>
> int logLevel;
> void Log(T...)(T t)
> {
>   if(logLevel > 3)
>     writefln(t);
> }
>
> class C
> {
>   char[] toString() { return "This " ~ " is a C"; }
> }
>
> void main()
> {
>  logLevel = 2;
>  auto c = new C;
>  Log(c);
> }
>
> In this case, the concatenation done in C.toString() is still evaluated, 
> even though we aren't going to use it.  That is what the lazy delegates 
> helps with.  You can 'fix' this by doing:

Just in case someone else notices, my example was wrong, C.toString is not 
evaluated because writefln is the one to evaluate it.

This is a better example:

char[] buildSomeString()
{
   return "hello " ~ "world";
}

Log(buildSomeString());

-Steve 




More information about the Digitalmars-d-learn mailing list