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

deadalnix deadalnix at gmail.com
Mon Jun 6 17:07:30 UTC 2022


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


More information about the Digitalmars-d mailing list