digitalmars.D.learn - Delayed const variable initialization
- Yuxuan Shui (9/9) Jul 13 2015 How can I do something like this?
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (2/11) Jul 13 2015 const(A) = condition ? func1() : func2();
- Yuxuan Shui (10/23) Jul 13 2015 Well I should have used a more complex example:
- ketmar (7/31) Jul 13 2015 as i wrote, what you really want is the ability to assign to `const`,=20
- Yuxuan Shui (4/26) Jul 13 2015 No! What I want to do is to assign to 'const' only ONCE. So it is
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (3/27) Jul 13 2015 You can encapsulate the two branches in nested functions, then
- ketmar (4/15) Jul 13 2015 p.s. also, this is not "initialization", but "assignment". `x` is=20
- Yuxuan Shui (4/20) Jul 13 2015 IMO this is too restrictive. Can't we just spit compile error for
- ketmar (8/11) Jul 13 2015 there is no "use before initialization", `x` is initialized at the=20
- Steven Schveighoffer (15/24) Jul 13 2015 auto xvalue() {
- Jesse Phillips (13/17) Jul 13 2015 Syntax is easy:
- Yuxuan Shui (2/20) Jul 13 2015 This is pretty neat. Thanks!
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
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
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: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); }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
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: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.=On Monday, 13 July 2015 at 07:11:33 UTC, Yuxuan Shui wrote:=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); }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();
Jul 13 2015
On Monday, 13 July 2015 at 09:05:25 UTC, ketmar wrote:On Mon, 13 Jul 2015 08:56:05 +0000, Yuxuan Shui wrote: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.On Monday, 13 July 2015 at 07:43:16 UTC, Marc Schütz 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.On Monday, 13 July 2015 at 07:11:33 UTC, Yuxuan Shui wrote: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); }[...]const(A) = condition ? func1() : func2();
Jul 13 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: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.On Mon, 13 Jul 2015 08:56:05 +0000, Yuxuan Shui wrote: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 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.
Jul 13 2015
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
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:You can encapsulate the two branches in nested functions, then you can still use the ternary operator.On Monday, 13 July 2015 at 07:11:33 UTC, Yuxuan Shui wrote: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); }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
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
On Monday, 13 July 2015 at 08:55:00 UTC, ketmar wrote:On Mon, 13 Jul 2015 07:11:32 +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?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.
Jul 13 2015
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
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
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 :) -SteveSyntax is easy: void main() { bool condition; const x = { if(condition) { return 1; } else { return 2; } }(); }
Jul 13 2015
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:This is pretty neat. Thanks!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 :) -SteveSyntax is easy: void main() { bool condition; const x = { if(condition) { return 1; } else { return 2; } }(); }
Jul 13 2015