zig/test/cases/switch_prong_implicit_cast.zig
Andrew Kelley 0ad1239522 rework enums and unions and their relationship to each other
* @enumTagName renamed to @tagName and it works on enums and
   union-enums
 * Remove the EnumTag type. Now there is only enum and union,
   and the tag type of a union is always an enum.
 * unions support specifying the tag enum type, and they support
   inferring an enum tag type.
 * Enums no longer support field types but they do support
   setting the tag values. Likewise union-enums when inferring
   an enum tag type support setting the tag values.
 * It is now an error for enums and unions to have 0 fields.
 * switch statements support union-enums

closes #618
2017-12-03 20:43:56 -05:00

25 lines
472 B
Zig

const assert = @import("std").debug.assert;
const FormValue = union(enum) {
One: void,
Two: bool,
};
error Whatever;
fn foo(id: u64) -> %FormValue {
switch (id) {
2 => FormValue { .Two = true },
1 => FormValue { .One = {} },
else => return error.Whatever,
}
}
test "switch prong implicit cast" {
const result = switch (%%foo(2)) {
FormValue.One => false,
FormValue.Two => |x| x,
};
assert(result);
}