aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-03-10 20:14:25 -0500
committerDave Gauer <dave@ratfactor.com>2021-03-10 20:14:25 -0500
commit42fde519e90fee5d21e0af553b086a4cab977148 (patch)
tree667169bef4f6a1fa3caf576118461a94c07b8353
parent89187e52a1f6a3df4740b6a136ea87afc2e30692 (diff)
downloadziglings-42fde519e90fee5d21e0af553b086a4cab977148.tar.gz
ziglings-42fde519e90fee5d21e0af553b086a4cab977148.tar.bz2
ziglings-42fde519e90fee5d21e0af553b086a4cab977148.tar.xz
ziglings-42fde519e90fee5d21e0af553b086a4cab977148.zip
add ex 57 unions 3
-rw-r--r--build.zig4
-rw-r--r--exercises/57_unions3.zig54
-rw-r--r--patches/patches/57_unions3.patch4
3 files changed, 62 insertions, 0 deletions
diff --git a/build.zig b/build.zig
index e8e160e..82a5786 100644
--- a/build.zig
+++ b/build.zig
@@ -288,6 +288,10 @@ const exercises = [_]Exercise{
.main_file = "56_unions2.zig",
.output = "Insect report! Ant alive is: true. Bee visited 16 flowers.",
},
+ .{
+ .main_file = "57_unions3.zig",
+ .output = "Insect report! Ant alive is: true. Bee visited 17 flowers.",
+ },
};
/// Check the zig version to make sure it can compile the examples properly.
diff --git a/exercises/57_unions3.zig b/exercises/57_unions3.zig
new file mode 100644
index 0000000..142180f
--- /dev/null
+++ b/exercises/57_unions3.zig
@@ -0,0 +1,54 @@
+//
+// 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"!
diff --git a/patches/patches/57_unions3.patch b/patches/patches/57_unions3.patch
new file mode 100644
index 0000000..17e27a2
--- /dev/null
+++ b/patches/patches/57_unions3.patch
@@ -0,0 +1,4 @@
+18c18
+< const Insect = union(InsectStat) {
+---
+> const Insect = union(enum) {