#Is there a way to create an empty tagged union in the switch parens?

1 messages · Page 1 of 1 (latest)

limpid rock
#

I get this error when creating an empty union type in the switch.

/tmp/zig_test > zig run src/main.zig                                                                                    1
src/main.zig:8:13: error: type 'main.main.My_union' does not support array initialization syntax
    switch (My_union{ .FOO }) {
            ^~~~~~~~

this works:

const std = @import("std");
pub fn main() !void {
    const My_union = union(enum) { FOO, BAR: u8 };
    const two: My_union = .FOO;
    loop2: switch (two) {
        .FOO => continue :loop2 My_union{ .BAR = 0x01 },
        .BAR => |val| std.debug.print("bar: {}\n", .{val}),
    }
}

With the new labeled switch it would be nice to write it without the need of an intermediate.
For the BAR version this is trivial with the array initialization syntax(switch (My_union{ .BAR = 0x43 }) {).
Is there a way to do this for FOO?
Using My_union.FOO does not work. I expect this is because it switches on the enum type instead of the value.
Resulting in the BAR case printing .BAR instead of the value.

dense yacht
#

or MyUnion{ .foo = {} }