digitalmars.D.learn - Static Initialization of Struct as UDA
- jmh530 (16/16) Jun 13 2017 The code below doesn't compile because "static variable z cannot
The code below doesn't compile because "static variable z cannot
be read at compile time". However, z is a static variable, so I
don't see why it wouldn't be available at compile-time.
Bug or am I missing something?
struct Bar
{
int x = 2;
int y;
}
static Bar z = {y:1};
void main()
{
z int d;
// Bar(2, 1) int d; //this compiles, but requires putting x
in there
}
Jun 13 2017
On Tuesday, 13 June 2017 at 22:04:48 UTC, jmh530 wrote:
The code below doesn't compile because "static variable z
cannot be read at compile time". However, z is a static
variable, so I don't see why it wouldn't be available at
compile-time.
Bug or am I missing something?
struct Bar
{
int x = 2;
int y;
}
static Bar z = {y:1};
void main()
{
z int d;
// Bar(2, 1) int d; //this compiles, but requires putting x
in there
}
use "static immutable" as storage class. static is not enough for
CT.
Jun 13 2017
On 06/14/2017 12:04 AM, jmh530 wrote:
The code below doesn't compile because "static variable z cannot be read
at compile time". However, z is a static variable, so I don't see why it
wouldn't be available at compile-time.
Bug or am I missing something?
struct Bar
{
int x = 2;
int y;
}
static Bar z = {y:1};
void main()
{
z int d;
// Bar(2, 1) int d; //this compiles, but requires putting x in there
}
No bug. `static` has no effect on module-level variables. `z` is a
normal mutable variable, not at all guaranteed to be constant. Make it
an `enum` or `immutable`.
Note that immutable doesn't guarantee compile-time constancy, either.
You can only use an `immutable` as a compile-time constant when it's
statically initialized.
Jun 13 2017
On Tuesday, 13 June 2017 at 22:16:37 UTC, ag0aep6g wrote:No bug. `static` has no effect on module-level variables. `z` is a normal mutable variable, not at all guaranteed to be constant. Make it an `enum` or `immutable`. Note that immutable doesn't guarantee compile-time constancy, either. You can only use an `immutable` as a compile-time constant when it's statically initialized.Appreciate the replies.
Jun 13 2017









Basile B. <b2.temp gmx.com> 