mirror of
https://github.com/ziglang/zig.git
synced 2024-11-26 23:22:44 +00:00
24f28753e6
Provide examples of various initializations.
31 lines
648 B
Zig
31 lines
648 B
Zig
const std = @import("std");
|
|
const expect = std.testing.expect;
|
|
|
|
const Variant = union(enum) {
|
|
int: i32,
|
|
boolean: bool,
|
|
|
|
// void can be omitted when inferring enum tag type.
|
|
none,
|
|
|
|
fn truthy(self: Variant) bool {
|
|
return switch (self) {
|
|
Variant.int => |x_int| x_int != 0,
|
|
Variant.boolean => |x_bool| x_bool,
|
|
Variant.none => false,
|
|
};
|
|
}
|
|
};
|
|
|
|
test "union method" {
|
|
var v1: Variant = .{ .int = 1 };
|
|
var v2: Variant = .{ .boolean = false };
|
|
var v3: Variant = .none;
|
|
|
|
try expect(v1.truthy());
|
|
try expect(!v2.truthy());
|
|
try expect(!v3.truthy());
|
|
}
|
|
|
|
// test
|