Thread overview |
---|
June 13, 2017 Static Initialization of Struct as UDA | ||||
---|---|---|---|---|
| ||||
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 } |
June 13, 2017 Re: Static Initialization of Struct as UDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | 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.
|
June 14, 2017 Re: Static Initialization of Struct as UDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | 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.
|
June 13, 2017 Re: Static Initialization of Struct as UDA | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | 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.
|
Copyright © 1999-2021 by the D Language Foundation