Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Created July 14, 2022 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hnakamur/07be36b371159642406ce1d5ef76d892 to your computer and use it in GitHub Desktop.
Save hnakamur/07be36b371159642406ce1d5ef76d892 to your computer and use it in GitHub Desktop.
debug failed attempt of creating union enum in Zig
const std = @import("std");
const expect = std.testing.expect;
const cell = struct {
v1: ?*Variant,
v2: ?*Variant,
};
const Variant = union(enum) {
int: i32,
boolean: bool,
cell: cell,
// 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.cell => false,
Variant.none => false,
};
}
};
test "union method" {
var v1 = Variant{ .int = 1 };
var v2 = Variant{ .boolean = false };
try expect(v1.truthy());
try expect(!v2.truthy());
}
test "create union" {
const allocator = std.testing.allocator;
var v1 = try allocator.create(Variant);
defer allocator.destroy(v1);
v1.* = .{ .int = 1 };
try expect(v1.truthy());
}
test "create union" {
const allocator = std.testing.allocator;
var top = try allocator.create(Variant);
defer allocator.destroy(top);
top.* = .{ .cell = cell{ .v1 = null, .v2 = null } };
var v1 = top;
var i: i32 = 0;
while (i < 3) : (i += 1) {
std.log.warn("setting v1.cell.v1: v1={}", .{@ptrToInt(v1)});
v1.cell.v1 = try allocator.create(Variant);
v1.cell.v1.?.* = .{ .int = i + 1 };
var v2 = try allocator.create(Variant);
std.log.warn("v2 created: {}", .{@ptrToInt(v2)});
defer allocator.destroy(v2);
defer std.log.warn("v2 destroy: {}", .{@ptrToInt(v2)});
v2.* = .{ .cell = cell{ .v1 = null, .v2 = null } };
v1.cell.v2 = v2;
v1 = v2;
}
std.log.warn("{}", .{top.cell.v2.?.cell.v1}); //
}
```
$ zig test create_union_enum.zig
Test [3/3] test "create union"... [default] (warn): setting v1.cell.v1: v1=140619163426816
[default] (warn): v2 created: 140619163426880
[default] (warn): v2 destroy: 140619163426880
[default] (warn): setting v1.cell.v1: v1=140619163426880
thread 2444827 panic: access of inactive union field
/home/hnakamur/ghq/github.com/hnakamur/learn-zig/create_union_enum.zig:57:11: 0x20c85d in test "create union" (test)
v1.cell.v1 = try allocator.create(Variant);
^
/home/hnakamur/zig/zig-0.10.0-dev.2989+4a28c1d5c/lib/test_runner.zig:79:28: 0x23496c in (root).main (test)
} else test_fn.func();
^
/home/hnakamur/zig/zig-0.10.0-dev.2989+4a28c1d5c/lib/std/start.zig:570:22: 0x20daf3 in std.start.posixCallMainAndExit (test)
root.main();
^
/home/hnakamur/zig/zig-0.10.0-dev.2989+4a28c1d5c/lib/std/start.zig:338:5: 0x20d8b2 in std.start._start (test)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
^
error: the following test command crashed:
zig-cache/o/c2daa2bc1a64e662f8f659d4cf0783d5/test /home/hnakamur/zig/zig-0.10.0-dev.2989+4a28c1d5c/zig
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment