Bug 7727 – "static initializer" for non-static unions too
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-03-18T06:00:00Z
Last change time
2013-10-05T00:33:52Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2012-03-18T06:00:53Z
struct Foo1 {
ushort bar2;
}
struct Foo2 {
union {
ubyte[2] bar1;
ushort bar2;
}
}
void main() {
immutable Foo1 foo1 = { bar2: 100 }; // OK
immutable Foo2 foo2 = { bar2: 100 }; // error
}
DMD 2.059 gives:
test.d(12): Error: variable test.main.foo2 is not a static and cannot have static initializer
(Maybe it's just a well known problem of unions not supported in CTFE?)
Comment #1 by verylonglogin.reg — 2013-09-30T07:12:58Z
The issue has nothing to do with immutability:
---
struct S { int i; double d; }
union U { int i; double d; }
void main()
{
S s = { d: 5 }; // OK
U u = { d: 5 }; // Error: variable main.main.u is not a static and cannot have static initializer
}
---
By the way, docs don't mention one can do such "static initialization" for non-static structs or unions. Anyway, the difference should be obliterated by allowing/disallowing both.