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

Steven Schveighoffer schveiguy at gmail.com
Wed Apr 22 15:15:24 UTC 2020


On 4/22/20 10:04 AM, kinke wrote:
> On Wednesday, 22 April 2020 at 12:30:52 UTC, Steven Schveighoffer wrote:
>> 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.
> 
> I disagree - `foo(myData.recomputeFromScratch())` has the same problem, 
> it's not obvious from the call site alone that the expensive operation 
> is deferred and might be skipped.

But if myData.recomputeFromScratch() has no side effects, what does it 
matter if not used or called?

For example, if you have:

auto foo(T)(bool cond, T trueValue, T falseValue)
{
    return cond ? trueValue : falseValue;
}

And you call like:

auto x = foo(cond, val, expensivePureCalc());

A sufficiently intelligent compiler is going to avoid calling 
expensivePureCalc when cond is false. Is that a problem?

Really the only use of lazy I use is in enforce. The lazy message might 
do GC allocations to generate a string that is thrown away if not used. 
But the string generation doesn't mutate anything, so it's not going to 
have an effect on the operation of the program if not called.

What I'm saying is if the DIP *enforced* that expression-to-delegate 
happens only for these cases, then there is no worry about whether the 
expression is evaluated or not.

-Steve


More information about the Digitalmars-d mailing list