Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 13, 2015 Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
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. |
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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();
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui Attachments: | 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.
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | 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);
}
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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?
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui Attachments: | 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.
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui Attachments: | On Mon, 13 Jul 2015 09:02:34 +0000, Yuxuan Shui wrote:
> 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?
there is no "use before initialization", `x` is initialized at the declaration site. this is by design -- there are no uninitialized variables in D.
also, you want `const` to stop being `const`. or, `const` to became C++ `const`. and C++ `const` is `Rebindable`. ;-)
this is the way variable initialization and constness are defined in D, and i don't think that design will change -- at least not in D2. ;-)
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Monday, 13 July 2015 at 09:05:25 UTC, ketmar wrote:
> 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:
>>>> [...]
>>>
>>> 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.
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.
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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.
|
July 13, 2015 Re: Delayed const variable initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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.
|
Copyright © 1999-2021 by the D Language Foundation