mirror of
https://github.com/ziglang/zig.git
synced 2024-11-30 09:02:32 +00:00
50 lines
1.0 KiB
Zig
50 lines
1.0 KiB
Zig
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
|
|
const E = enum {
|
|
one,
|
|
two,
|
|
three,
|
|
};
|
|
|
|
const U = union(E) {
|
|
one: i32,
|
|
two: f32,
|
|
three,
|
|
};
|
|
|
|
const U2 = union(enum) {
|
|
a: void,
|
|
b: f32,
|
|
|
|
fn tag(self: U2) usize {
|
|
switch (self) {
|
|
.a => return 1,
|
|
.b => return 2,
|
|
}
|
|
}
|
|
};
|
|
|
|
test "coercion between unions and enums" {
|
|
const u = U{ .two = 12.34 };
|
|
const e: E = u; // coerce union to enum
|
|
try expect(e == E.two);
|
|
|
|
const three = E.three;
|
|
const u_2: U = three; // coerce enum to union
|
|
try expect(u_2 == E.three);
|
|
|
|
const u_3: U = .three; // coerce enum literal to union
|
|
try expect(u_3 == E.three);
|
|
|
|
const u_4: U2 = .a; // coerce enum literal to union with inferred enum tag type.
|
|
try expect(u_4.tag() == 1);
|
|
|
|
// The following example is invalid.
|
|
// error: coercion from enum '@TypeOf(.enum_literal)' to union 'test_coerce_unions_enum.U2' must initialize 'f32' field 'b'
|
|
//var u_5: U2 = .b;
|
|
//try expect(u_5.tag() == 2);
|
|
}
|
|
|
|
// test
|