mirror of
https://github.com/ziglang/zig.git
synced 2025-12-24 15:13:08 +00:00
* AIR: add `get_union_tag` instruction
- implement in LLVM backend
* Sema: implement == and != for union and enum literal
- Also implement coercion from union to its own tag type
* Value: implement hashing for union values
The motivating example is this snippet:
comptime assert(@typeInfo(T) == .Float);
This was the next blocker for stage2 building compiler-rt.
Now it is switch at compile-time on an integer.
35 lines
673 B
Zig
35 lines
673 B
Zig
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
const expectEqual = std.testing.expectEqual;
|
|
const Tag = std.meta.Tag;
|
|
|
|
const Foo = union {
|
|
float: f64,
|
|
int: i32,
|
|
};
|
|
|
|
test "basic unions" {
|
|
var foo = Foo{ .int = 1 };
|
|
try expect(foo.int == 1);
|
|
foo = Foo{ .float = 12.34 };
|
|
try expect(foo.float == 12.34);
|
|
}
|
|
|
|
test "init union with runtime value" {
|
|
var foo: Foo = undefined;
|
|
|
|
setFloat(&foo, 12.34);
|
|
try expect(foo.float == 12.34);
|
|
|
|
setInt(&foo, 42);
|
|
try expect(foo.int == 42);
|
|
}
|
|
|
|
fn setFloat(foo: *Foo, x: f64) void {
|
|
foo.* = Foo{ .float = x };
|
|
}
|
|
|
|
fn setInt(foo: *Foo, x: i32) void {
|
|
foo.* = Foo{ .int = x };
|
|
}
|