lazy variables cannot be lvalues - why?
Jonathan M Davis
jmdavisProg at gmx.com
Mon Nov 1 09:20:23 PDT 2010
On Monday, November 01, 2010 08:57:09 Adam Cigánek wrote:
> Hello,
>
> why is the following code illegal?
>
>
> import std.stdio;
>
> void delegate() fun;
>
> void capture(lazy void f) {
> fun = &f;
> }
>
> void main() {
> capture(writeln("hello"));
> fun();
> }
>
>
> It says "Error: lazy variables cannot be lvalues", pointing to the
> "fun = &f" line.
>
> It can be worked around by rewriting it like this:
>
> void capture(lazy void f) {
> fun = delegate void() { f(); };
> }
>
> So it's not big deal, just a minor inconvenience. But still, why is it
> illegal? According to the docs
> (http://www.digitalmars.com/d/2.0/lazy-evaluation.html), lazy
> expressions are implicitly converted to delegates, so it seems to me
> that it should work.
>
> adam.
1. I 'm stunned that the compiler doesn't complain about you declaring f as
void. It strikes me as a bug with lazy. You can't declare variables of type
void. It makes no sense.
2. A lazy parameter is for all intents an purposes the exact same thing as a
non-lazy parameter except that it's not actually calculated until the function
is called. The fact that a delegate to make it lazy is used is an implementation
detail. Use typeof on it, and you'll notice that its type is the same as if it
weren't lazy, not a delegate.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list