0ad1239522
* @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
28 lines
702 B
Zig
28 lines
702 B
Zig
const assert = @import("std").debug.assert;
|
|
const mem = @import("std").mem;
|
|
const fmt = @import("std").fmt;
|
|
|
|
const ET = union(enum) {
|
|
SINT: i32,
|
|
UINT: u32,
|
|
|
|
pub fn print(a: &const ET, buf: []u8) -> %usize {
|
|
return switch (*a) {
|
|
ET.SINT => |x| { fmt.formatIntBuf(buf, x, 10, false, 0) },
|
|
ET.UINT => |x| { fmt.formatIntBuf(buf, x, 10, false, 0) },
|
|
}
|
|
}
|
|
};
|
|
|
|
test "enum with members" {
|
|
const a = ET { .SINT = -42 };
|
|
const b = ET { .UINT = 42 };
|
|
var buf: [20]u8 = undefined;
|
|
|
|
assert(%%a.print(buf[0..]) == 3);
|
|
assert(mem.eql(u8, buf[0..3], "-42"));
|
|
|
|
assert(%%b.print(buf[0..]) == 2);
|
|
assert(mem.eql(u8, buf[0..2], "42"));
|
|
}
|