Delayed const variable initialization

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 13 10:31:20 PDT 2015


On Monday, 13 July 2015 at 17:18:53 UTC, Jesse Phillips wrote:
> On Monday, 13 July 2015 at 14:41:36 UTC, Steven Schveighoffer 
> wrote:
>> You can also do a temporary lambda that you call immediately, 
>> but I'm not 100% sure of the syntax. Someone will chime in 
>> with the answer :)
>>
>> -Steve
>
> Syntax is easy:
>
> void main() {
>     bool condition;
>     const x = {
>         if(condition) {
>              return 1;
>           } else {
>              return 2;
>           }
>     }();
> }

This is pretty neat. Thanks!


More information about the Digitalmars-d-learn mailing list