2016-12-20 14:50:32 +08:00
|
|
|
fn enumType() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
const foo1 = Foo.One {13};
|
|
|
|
const foo2 = Foo.Two { Point { .x = 1234, .y = 5678, }};
|
|
|
|
const bar = Bar.B;
|
|
|
|
|
|
|
|
assert(bar == Bar.B);
|
|
|
|
assert(@memberCount(Foo) == 3);
|
|
|
|
assert(@memberCount(Bar) == 4);
|
|
|
|
const expected_foo_size = 16 + @sizeOf(usize);
|
|
|
|
assert(@sizeOf(Foo) == expected_foo_size);
|
|
|
|
assert(@sizeOf(Bar) == 1);
|
|
|
|
}
|
2016-12-22 10:49:05 +08:00
|
|
|
|
|
|
|
fn enumAsReturnValue () {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
switch (returnAnInt(13)) {
|
|
|
|
Foo.One => |value| assert(value == 13),
|
|
|
|
else => @unreachable(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 14:50:32 +08:00
|
|
|
const Point = struct {
|
|
|
|
x: u64,
|
|
|
|
y: u64,
|
|
|
|
};
|
|
|
|
const Foo = enum {
|
|
|
|
One: i32,
|
|
|
|
Two: Point,
|
|
|
|
Three: void,
|
|
|
|
};
|
|
|
|
const Bar = enum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
D,
|
|
|
|
};
|
|
|
|
|
2016-12-22 10:49:05 +08:00
|
|
|
fn returnAnInt(x: i32) -> Foo {
|
|
|
|
Foo.One { x }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-22 14:20:08 +08:00
|
|
|
fn constantEnumWithPayload() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
var empty = AnEnumWithPayload.Empty;
|
|
|
|
var full = AnEnumWithPayload.Full {13};
|
|
|
|
shouldBeEmpty(empty);
|
|
|
|
shouldBeNotEmpty(full);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shouldBeEmpty(x: AnEnumWithPayload) {
|
|
|
|
switch (x) {
|
|
|
|
AnEnumWithPayload.Empty => {},
|
|
|
|
else => @unreachable(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shouldBeNotEmpty(x: AnEnumWithPayload) {
|
|
|
|
switch (x) {
|
|
|
|
AnEnumWithPayload.Empty => @unreachable(),
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const AnEnumWithPayload = enum {
|
|
|
|
Empty,
|
|
|
|
Full: i32,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-26 15:36:04 +08:00
|
|
|
const Number = enum {
|
|
|
|
Zero,
|
|
|
|
One,
|
|
|
|
Two,
|
|
|
|
Three,
|
|
|
|
Four,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn enumToInt() {
|
|
|
|
@setFnTest(this);
|
|
|
|
|
|
|
|
shouldEqual(Number.Zero, 0);
|
|
|
|
shouldEqual(Number.One, 1);
|
|
|
|
shouldEqual(Number.Two, 2);
|
|
|
|
shouldEqual(Number.Three, 3);
|
|
|
|
shouldEqual(Number.Four, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn shouldEqual(n: Number, expected: usize) {
|
|
|
|
assert(usize(n) == expected);
|
|
|
|
}
|
|
|
|
|
2016-12-26 16:44:59 +08:00
|
|
|
|
2016-12-26 15:36:04 +08:00
|
|
|
// TODO import from std
|
2016-12-20 14:50:32 +08:00
|
|
|
fn assert(ok: bool) {
|
|
|
|
if (!ok)
|
|
|
|
@unreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|