Do you think if statement as expression would be nice to have in D?

SealabJaster sealabjaster at gmail.com
Mon Jun 6 22:25:37 UTC 2022


On Monday, 6 June 2022 at 17:07:30 UTC, deadalnix wrote:
> On Saturday, 4 June 2022 at 21:17:56 UTC, SealabJaster wrote:
>> On Saturday, 4 June 2022 at 19:54:48 UTC, Dukc wrote:
>>> ```D
>>> auto number = 200;
>>> auto myVar = type == "as-is"?
>>>   number:
>>>   { number *= 2;
>>>     number += 2;
>>>     number /= 2;
>>>     number = number > 300 ? 200 : 100;
>>>     return number;
>>>   }();
>>> ```
>>
>> This next question comes from a place of ignorance: What is 
>> the codegen like for this code? Would is allocate a closure on 
>> the GC before performing the execution, or are the compilers 
>> smart enough to inline the entire thing?
>
> LDC is able to inline the lambda and then optimize away the 
> allocation. DMD is not.
>
> https://godbolt.org/z/5vMcz4s14

Ah nice, then yeah I don't see any real downsides to just using 
lambdas like that :)


More information about the Digitalmars-d mailing list