www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Delayed const variable initialization

reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
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.
Jul 13 2015
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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();
Jul 13 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
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); }
Jul 13 2015
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 13 Jul 2015 08:56:05 +0000, Yuxuan Shui wrote:

 On Monday, 13 July 2015 at 07:43:16 UTC, Marc Sch=C3=BCtz 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 =3D func1();
 } else {
   x =3D func2();
 }

 This is a valid use case, and to make this work I'll have to use
 Rebindable, which is really inconvenient.
const(A) =3D condition ? func1() : func2();
=20 Well I should have used a more complex example: =20 const(A) x; if (condition) { //Do some really complicated stuff here x =3D func1(results); } else { //Here too x =3D func2(results); }
as i wrote, what you really want is the ability to assign to `const`,=20 which is forbidden in D. initialization of `x` happens at the declaration=20 site: `const(A) x =3D A.init;` so, no, the design of `const` will not allow you to assign to consts.=20 remember, it's not c++ const. chances are, you don't need that `const` at=20 all -- as you clearly wants to modify a variable.=
Jul 13 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
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.
Jul 13 2015
next sibling parent "tcak" <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
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.
Jul 13 2015
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 13 Jul 2015 09:08:13 +0000, Yuxuan Shui wrote:

 No! What I want to do is to assign to 'const' only ONCE. So it is
 basically an initialization.
it is forbidden to do assigning to `const`. and D has *no* "uninitialized=20 variable" thingy, even `x =3D void` is initialization too (in technical=20 sense). so you want c++-like const, which `Rebindable` does.
 What I really want is to detach initialization from definition.
you can't. it's by design.=
Jul 13 2015
prev sibling parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Jul 13 2015
prev sibling next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Mon, 13 Jul 2015 07:11:32 +0000, Yuxuan Shui wrote:

 How can I do something like this?
=20
 const(A) x;
 if (condition) {
    x =3D func1();
 } else {
    x =3D func2();
 }
=20
 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=20 initialized to default value here, and then compiler will not allow=20 assignments. unlike c/c++, in D variables are always initialized.=
Jul 13 2015
parent reply "Yuxuan Shui" <yshuiv7 gmail.com> writes:
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?
Jul 13 2015
parent ketmar <ketmar ketmar.no-ip.org> writes:
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=20 declaration site. this is by design -- there are no uninitialized=20 variables in D. also, you want `const` to stop being `const`. or, `const` to became C++=20 `const`. and C++ `const` is `Rebindable`. ;-) this is the way variable initialization and constness are defined in D,=20 and i don't think that design will change -- at least not in D2. ;-)=
Jul 13 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/13/15 3:11 AM, 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.
auto xvalue() { if(condition) { return func1(); } else { return func2(); } } const(A) x = xvalue(); Essentially you can assign on initialization, so you have to encapsulate the initialization into one call. D allows nested functions quite easily, so you can do this. You can also do a temporary lambda that you call immediately, but I'm not 100% sure of the syntax. Someone will chime in with the answer :) -Steve
Jul 13 2015
parent reply "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Monday, 13 July 2015 at 14:41:36 UTC, Steven Schveighoffer 
wrote:
 You can also do a temporary lambda that you call immediately, 
 but I'm not 100% sure of the syntax. Someone will chime in with 
 the answer :)

 -Steve
Syntax is easy: void main() { bool condition; const x = { if(condition) { return 1; } else { return 2; } }(); }
Jul 13 2015
parent "Yuxuan Shui" <yshuiv7 gmail.com> writes:
On Monday, 13 July 2015 at 17:18:53 UTC, Jesse Phillips wrote:
 On Monday, 13 July 2015 at 14:41:36 UTC, Steven Schveighoffer 
 wrote:
 You can also do a temporary lambda that you call immediately, 
 but I'm not 100% sure of the syntax. Someone will chime in 
 with the answer :)

 -Steve
Syntax is easy: void main() { bool condition; const x = { if(condition) { return 1; } else { return 2; } }(); }
This is pretty neat. Thanks!
Jul 13 2015