How to hand in a closure variable

Bienlein jeti789 at web.de
Mon Apr 7 02:47:47 PDT 2014


On Friday, 4 April 2014 at 19:56:14 UTC, Jesse Phillips wrote:
> On Friday, 4 April 2014 at 15:13:25 UTC, Bienlein wrote:
>> What I was actually looking for was how to get this to work:
>>
>> immutable int b = if(1 == 1) { return 123; } else { return 
>> 456; };
>>
>> But I'm happy enough with the solution through a delegate.
>
> What bearophile said, or:
>
> immutable int b = {if(1 == 1) { return 123; } else { return 
> 456; }}();

Thanks, that's it! Now I can also do what I initialliy wanted to 
(e.g. have several lines of code in the expression blocks):

immutable int b = {
     if(1 == 1) {
         writeln("123");
         return 123;
     } else {
         writeln("456");
         return 456;
     }
}();


More information about the Digitalmars-d-learn mailing list