Delayed const variable initialization

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 13 04:03:32 PDT 2015


On Monday, 13 July 2015 at 08:56:07 UTC, Yuxuan Shui wrote:
> On Monday, 13 July 2015 at 07:43:16 UTC, Marc Schütz wrote:
>> On Monday, 13 July 2015 at 07:11:33 UTC, Yuxuan Shui wrote:
>>> How can I do something like this?
>>>
>>> const(A) x;
>>> if (condition) {
>>>   x = func1();
>>> } else {
>>>   x = func2();
>>> }
>>>
>>> This is a valid use case, and to make this work I'll have to 
>>> use Rebindable, which is really inconvenient.
>>
>> const(A) = condition ? func1() : func2();
>
> Well I should have used a more complex example:
>
> const(A) x;
> if (condition) {
>   //Do some really complicated stuff here
>   x = func1(results);
> } else {
>   //Here too
>   x = func2(results);
> }

You can encapsulate the two branches in nested functions, then 
you can still use the ternary operator.


More information about the Digitalmars-d-learn mailing list