Discussion Thread: DIP 1033--Implicit Conversion of Expressions to Delegates--Community Review Round 1

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 22 12:30:52 UTC 2020


On 4/22/20 7:56 AM, kinke wrote:
> I don't think this is a good idea. One of the problems with `lazy` IMO 
> is that one needs good IDE support to somehow highlight that the 
> argument expression isn't evaluated before the call, and might not be 
> evaluated at all:
> 
> // in some other module:
> void foo(lazy int i) {}
> 
> void main()
> {
>      int i = 1;
>      foo(++i);
>      assert(i == 1); // what? not incremented above? ah yeah, lazy and 
> not evaluated at all by foo
> }

This is more of a problem of lazy parameters that mutate something. Most 
of the time, that isn't the use case of lazy -- it's to prevent calling 
some expensive thing when it's not needed. Most definitely this DIP 
would make that problem worse.

What could be something that helps is enforcing that expressions to 
delegate implicit conversions require no mutation. I'm not sure how that 
could be enforced, but possibly requiring only pure function calls on 
const data (including the context).

It wouldn't be a breaking change, because currently expressions don't 
bind to delegates, so we can enforce that rule there, then deprecate 
mutating expressions on lazy parameters, and finally remove lazy.

> 
> The proposal would extend that problem to regular delegate params too. 
> So I find the explicit version (`() => ++i`) better to make that clear. 
> I don't think the 'overhead' of putting an additional `() =>` in front 
> of the expression justifies introducing the proposed implicit 
> convertibility.

I've never had much of an issue with the verbosity of delegates, 
especially since the () => form came about.

What I HAVE had an issue with is not being able to pass a delegate into 
a lazy parameter. e.g.:

void foo(lazy int x);

foo(() {some; large; calculation; return result; }); // no good.
foo(() {...}()); // works but, is a delegate that calls a delegate, ugh.

To that end, would it be possible to add lazy binding to a delegate in 
the same DIP? The more we make lazy behave like a delegate, the easier 
it will be to remove lazy.

-Steve


More information about the Digitalmars-d mailing list