diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
commit | 6ad9774189fbd64b2f2c9519f4513ab34b0c3809 (patch) | |
tree | d6c90700131d5b28e898881f13e2a05612e4703f /exercises/57_unions3.zig | |
parent | be36352572ddb18218e1830e49316c259dea5e8c (diff) | |
download | ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.gz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.bz2 ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.xz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.zip |
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate
state of soteriological release and no more exercises will be needed.
The cycle will be complete. All that will be left is perfect quietude,
freedom, and highest happiness.
Diffstat (limited to 'exercises/57_unions3.zig')
-rw-r--r-- | exercises/57_unions3.zig | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/exercises/57_unions3.zig b/exercises/57_unions3.zig deleted file mode 100644 index 142180f..0000000 --- a/exercises/57_unions3.zig +++ /dev/null @@ -1,54 +0,0 @@ -// -// With tagged unions, it gets EVEN BETTER! If you don't have a -// need for a separate enum, you can define an inferred enum with -// your union all in one place. Just use the 'enum' keyword in -// place of the tag type: -// -// const Foo = union(enum) { -// small: u8, -// medium: u32, -// large: u64, -// }; -// -// Let's convert Insect. Doctor Zoraptera has already deleted the -// explicit InsectStat enum for you! -// -const std = @import("std"); - -const Insect = union(InsectStat) { - flowers_visited: u16, - still_alive: bool, -}; - -pub fn main() void { - var ant = Insect{ .still_alive = true }; - var bee = Insect{ .flowers_visited = 17 }; - - std.debug.print("Insect report! ", .{}); - - printInsect(ant); - printInsect(bee); - - std.debug.print("\n", .{}); -} - -fn printInsect(insect: Insect) void { - switch (insect) { - .still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}), - .flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}), - } -} - -// Inferred enums are neat, representing the tip of the iceberg -// in the relationship between enums and unions. You can actually -// coerce a union TO an enum (which gives you the active field -// from the union as an enum). What's even wilder is that you can -// coerce an enum to a union! But don't get too excited, that -// only works when the union type is one of those weird zero-bit -// types like void! -// -// Tagged unions, as with most ideas in computer science, have a -// long history going back to the 1960s. However, they're only -// recently becoming mainstream, particularly in system-level -// programming languages. You might have also seen them called -// "variants", "sum types", or even "enums"! |