Assigning value to a lazy parameter in a function call

Vidar Wahlberg canidae at exent.net
Fri May 11 13:45:38 PDT 2012


Wasn't easy to find a short good description of the issue in the 
subject, but here's some code to illustrate my "concern":
---
import std.stdio;
void log(T...)(lazy string message, lazy T t) {
         debug writefln(message, t);
}
void main() {
         int a = 42;
         writefln("The meaning of life is %s", a);
         log("Incrementing the meaning of life: %s", ++a);
         writefln("The meaning of life is now %s", a);
}
---

I often call functions where one of the parameters may be an integer 
which i post/pre increment/decrement. However, that can be quite risky 
if the parameter is defined as "lazy" as shown above.
The value of "a" above after calling "log" depends on whether you've 
compiled with "-debug" or not, this may not be very obvious and may take 
some by surprise.

Perhaps the compiler should print out a warning when you're assigning a 
value to a lazy parameter in a function call?


More information about the Digitalmars-d-learn mailing list