I have attached an example to this bug report. For whatever reason, when using an anonymous union inside a struct and initializing a value from a constructor argument, I'm seeing the wrong value. I first noticed it when trying to force execution of my JSON library's parseJSON function at compile time using enum, and I kept getting an empty object back.
This only seems to happen when mixing types in the union like numbers and strings, arrays and numbers, etc. I ran the example with rdmd 2.062.
Comment #1 by devw0rp — 2013-04-27T13:33:18Z
Created attachment 1211
An example of the bug mentioned
Comment #2 by code — 2013-04-27T14:48:19Z
Works in HEAD, but it would still be interesting to know what fixed the bug.
Comment #3 by yebblies — 2013-11-21T08:20:08Z
Test case:
import std.stdio;
import std.conv;
struct Test {
union {
string _str;
long _int;
ulong _uint;
}
this(long val) {
this._int = val;
}
string toString() const {
return to!string(_int);
}
}
Test foo() {
return Test(3);
}
void main(string[] argv) {
enum x = foo();
// Writes 0
writeln(x);
auto y = foo();
// Writes 3
writeln(y);
}