|
@@ -32,29 +32,29 @@ const std = @import("std");
|
|
|
|
|
|
const InsectStat = enum { flowers_visited, still_alive };
|
|
|
|
|
|
-const Insect = union(InsectStat) {
|
|
|
- flowers_visited: u16,
|
|
|
- still_alive: bool,
|
|
|
+const Insect = union(InsectStat){
|
|
|
+ flowers_visited: u16,
|
|
|
+ still_alive: bool,
|
|
|
};
|
|
|
|
|
|
-pub fn main() void {
|
|
|
- const ant = Insect{ .still_alive = true };
|
|
|
- const bee = Insect{ .flowers_visited = 16 };
|
|
|
-
|
|
|
- std.debug.print("Insect report! ", .{});
|
|
|
-
|
|
|
- // Could it really be as simple as just passing the union?
|
|
|
- printInsect(???);
|
|
|
- printInsect(???);
|
|
|
-
|
|
|
- std.debug.print("\n", .{});
|
|
|
+pub fn main() void{
|
|
|
+ const ant = Insect{ .still_alive = true };
|
|
|
+ const bee = Insect{ .flowers_visited = 16 };
|
|
|
+
|
|
|
+ std.debug.print("Insect report! ", .{});
|
|
|
+
|
|
|
+ // Could it really be as simple as just passing the union?
|
|
|
+ printInsect(ant);
|
|
|
+ printInsect(bee);
|
|
|
+
|
|
|
+ std.debug.print("\n", .{});
|
|
|
}
|
|
|
|
|
|
-fn printInsect(insect: Insect) void {
|
|
|
- switch (???) {
|
|
|
- .still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}),
|
|
|
- .flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}),
|
|
|
- }
|
|
|
+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}),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// By the way, did unions remind you of optional values and errors?
|
|
@@ -62,3 +62,4 @@ fn printInsect(insect: Insect) void {
|
|
|
// union types". Now we can add our own unions to the mix to handle
|
|
|
// whatever situations we might encounter:
|
|
|
// union(Tag) { value: u32, toxic_ooze: void }
|
|
|
+
|