Delayed const variable initialization

Yuxuan Shui via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 13 02:02:34 PDT 2015


On Monday, 13 July 2015 at 08:55:00 UTC, ketmar wrote:
> On Mon, 13 Jul 2015 07:11:32 +0000, 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.
>
> p.s. also, this is not "initialization", but "assignment". `x` 
> is initialized to default value here, and then compiler will 
> not allow assignments. unlike c/c++, in D variables are always 
> initialized.

IMO this is too restrictive. Can't we just spit compile error for 
uses before initialization, and allow const variables to be 
initialized somewhere other than where it is defined?


More information about the Digitalmars-d-learn mailing list