lazy variables cannot be lvalues - why?

Stewart Gordon smjg_1998 at
Mon Nov 1 10:13:14 PDT 2010

On 01/11/2010 15:57, Adam Cigánek wrote:
>    void capture(lazy void f) {
>      fun =&f;
>    }
> It says "Error: lazy variables cannot be lvalues", pointing to the
> "fun =&f" line.

Because f doesn't have an address.  It's just an expression that's 
evaluated where it's used.  It's true that the function in which the 
compiler wraps f has an address, but that isn't the same.

> 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
> (, lazy
> expressions are implicitly converted to delegates, so it seems to me
> that it should work.

This is inaccurate or at least badly worded.  What's really meant is 
that the compiler converts the lazy expression to a delegate. 
Unfortunately, there's no way at the moment to extract that delegate, 
though I do wish D explicitly supported & as a way of doing it.

There's a workaround: declaring the function instead as

     void capture(void delegate()[1] f...)

which is the special case given on
under "Lazy Variadic Functions".


More information about the Digitalmars-d-learn mailing list