[Issue 2939] lazy evaluation not invoked for lambda function

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Tue Aug 9 14:30:47 PDT 2016


https://issues.dlang.org/show_bug.cgi?id=2939

ag0aep6g at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |ag0aep6g at gmail.com
         Resolution|---                         |INVALID

--- Comment #10 from ag0aep6g at gmail.com ---
(In reply to Jarrett Billingsley from comment #1)
> void f(lazy void dg)
> {
> 	dg();
> }
> 
> void main()
> {
> 	void foo() { Stdout.formatln("o hai"); }
> 	f(foo);

`foo` is a call here. A delegate would be `&foo`. The call is deferred, because
f's parameter is lazy. `dg()` in f executes the call.

> 	f({Stdout.formatln("lol wut");});

This passes a delegate to f, lazily of course. So `dg()` in f evaluates to the
delegate. The delegate itself is never called.

> }

(In reply to Jarrett Billingsley from comment #6)
> Yes, I'm pretty sure that's what's happening.  But there are two issues:
> 
> (1) It's extremely counterintuitive, easy to forget, and when you invariably
> get bitten by it, the compiler and runtime give no help diagnosing the
> problem.

That warrants an enhancement request, at best. If everything works as
specified, then it's not a bug. Personally, I don't think the behavior here is
particularly surprising.

> (2) Why does passing a delegate reference work, but not a lambda?  They are
> *the same type* and you'd expect the compiler to do *the same thing* with
> both.

Not the case as explained above.

I'm closing this as INVALID. If, after seven years, anyone still thinks that
this issue should be addressed, please file another issue and make it an
enhancement request. Of course, if you disagree with my assessment here, feel
free to reopen this bug.

--


More information about the Digitalmars-d-bugs mailing list