Delayed const variable initialization

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 13 02:18:23 PDT 2015


On Monday, 13 July 2015 at 09:08:14 UTC, Yuxuan Shui wrote:
> On Monday, 13 July 2015 at 09:05:25 UTC, ketmar wrote:
>> On Mon, 13 Jul 2015 08:56:05 +0000, Yuxuan Shui wrote:
>>
>>> [...]
>>
>> as i wrote, what you really want is the ability to assign to 
>> `const`, which is forbidden in D. initialization of `x` 
>> happens at the declaration site: `const(A) x = A.init;`
>>
>> so, no, the design of `const` will not allow you to assign to 
>> consts. remember, it's not c++ const. chances are, you don't 
>> need that `const` at all -- as you clearly wants to modify a 
>> variable.
>
> No! What I want to do is to assign to 'const' only ONCE. So it 
> is basically an initialization.
>
> What I really want is to detach initialization from definition.

As a solution to your (my guessed) problem, you can use @property 
for this.

Make x private. Set it based on condition somewhere. Then define 
a getter for x as public.


More information about the Digitalmars-d-learn mailing list