Assigning value to a lazy parameter in a function call

Chris Cain clcain at uncg.edu
Fri May 11 16:22:05 PDT 2012


On Friday, 11 May 2012 at 23:07:31 UTC, Vidar Wahlberg wrote:
> Thank you for the detailed answer.
> I still suspect this can fool some people, and (in my 
> ignorance) I couldn't (and still can't, to be honest) really 
> see when you would want to assign a variable in a lazy 
> parameter, I would expect that to be far more often an error 
> from the coder rather than intended behavior. I'm not here to 
> argue though, I just wanted to express my thoughts on the issue 
> :)

No problem. I hope it's become a bit more clear what lazy is for.

++a isn't assigning a. It's causing a to mutate in place (which, 
is just changing state). And lazy parameters are all about 
holding the change of state off until it's necessary.

The biggest "gotcha" about lazy parameters isn't going to be 
that, though... it'll be something like this:

-=-=-
void funct(lazy int x) {
     writeln(x, " ", x);
}

void main() {
     int a = 0;
     funct(++a); // prints 1, 2
     // a is now 2, even though there's only one ++a
}
-=-=-

But keeping in mind that lazy is just convenient syntax sugar for 
a delegate function, the reason why this behaves like this is 
clearer.


More information about the Digitalmars-d-learn mailing list