Static Initialization of Struct as UDA
ag0aep6g via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jun 13 15:16:37 PDT 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.
More information about the Digitalmars-d-learn
mailing list