Comment #0 by andrej.mitrovich — 2011-01-18T09:36:41Z
Example 1:
import std.stdio;
enum WindowSizes : QSize
{
Minimum = QSize(10)
}
struct QSize
{
int store;
this(int x)
{
store = x + 10;
}
}
void main()
{
auto foo = WindowSizes.Minimum;
assert(foo.store == 10); // What?
auto bar = QSize(10);
assert(bar.store == 20);
}
The constructor is never called for the enum. It does field by field assignments instead, as shown in this case:
import std.stdio;
enum WindowSizes : Inverted
{
Minimum = Inverted(10, 20)
}
struct Inverted
{
int x;
int y;
this(int in_x, int in_y)
{
x = in_y;
y = in_x;
}
}
void main()
{
auto foo = WindowSizes.Minimum;
assert(foo.x == 10);
assert(foo.y == 20);
auto bar = Inverted(10, 20);
assert(bar.x == 20);
assert(bar.y == 10);
}
Field assignments should be disabled when a constructor is present.
Comment #1 by andrej.mitrovich — 2011-01-18T09:38:13Z
*I meant constructing a struct with field assignment*, not regular field assignment after a struct was constructed.
Comment #2 by andrej.mitrovich — 2012-01-04T07:33:27Z
The following has now changed into a CT error:
enum Sizes : Size {
Minimum = Size(10)
}
struct Size {
int store;
}
void main()
{
}
test.d(4): Error: Integer constant expression expected instead of Size(10)
test.d(4): Error: Integer constant expression expected instead of Size(10)
Comment #3 by andrej.mitrovich — 2013-09-17T13:03:30Z
Since 2.063.2 (likely due to enum fixes as well), this code now works properly. Gotta give a huge thanks to Don and Kenji (and others) for enabling CTFE to work great these days.