Delayed const variable initialization
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Jul 13 02:05:25 PDT 2015
On Mon, 13 Jul 2015 08:56:05 +0000, 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);
> }
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150713/99126f07/attachment.sig>
More information about the Digitalmars-d-learn
mailing list