|
@@ -42,35 +42,36 @@ const std = @import("std");
|
|
|
// Insects will be represented by either bees or ants. Bees store
|
|
|
// the number of flowers they've visited that day and ants just
|
|
|
// store whether or not they're still alive.
|
|
|
-const Insect = union {
|
|
|
- flowers_visited: u16,
|
|
|
- still_alive: bool,
|
|
|
+const Insect = union{
|
|
|
+ flowers_visited: u16,
|
|
|
+ still_alive: bool,
|
|
|
};
|
|
|
|
|
|
// Since we need to specify the type of insect, we'll use an
|
|
|
// enum (remember those?).
|
|
|
const AntOrBee = enum { a, b };
|
|
|
|
|
|
-pub fn main() void {
|
|
|
- // We'll just make one bee and one ant to test them out:
|
|
|
- const ant = Insect{ .still_alive = true };
|
|
|
- const bee = Insect{ .flowers_visited = 15 };
|
|
|
-
|
|
|
- std.debug.print("Insect report! ", .{});
|
|
|
-
|
|
|
- // Oops! We've made a mistake here.
|
|
|
- printInsect(ant, AntOrBee.c);
|
|
|
- printInsect(bee, AntOrBee.c);
|
|
|
-
|
|
|
- std.debug.print("\n", .{});
|
|
|
+pub fn main() void{
|
|
|
+ // We'll just make one bee and one ant to test them out:
|
|
|
+ const ant = Insect{ .still_alive = true };
|
|
|
+ const bee = Insect{ .flowers_visited = 15 };
|
|
|
+
|
|
|
+ std.debug.print("Insect report! ", .{});
|
|
|
+
|
|
|
+ // Oops! We've made a mistake here.
|
|
|
+ printInsect(ant, AntOrBee.a);
|
|
|
+ printInsect(bee, AntOrBee.b);
|
|
|
+
|
|
|
+ std.debug.print("\n", .{});
|
|
|
}
|
|
|
|
|
|
// Eccentric Doctor Zoraptera says that we can only use one
|
|
|
// function to print our insects. Doctor Z is small and sometimes
|
|
|
// inscrutable but we do not question her.
|
|
|
-fn printInsect(insect: Insect, what_it_is: AntOrBee) void {
|
|
|
- switch (what_it_is) {
|
|
|
- .a => std.debug.print("Ant alive is: {}. ", .{insect.still_alive}),
|
|
|
- .b => std.debug.print("Bee visited {} flowers. ", .{insect.flowers_visited}),
|
|
|
- }
|
|
|
+fn printInsect(insect: Insect, what_it_is: AntOrBee) void{
|
|
|
+ switch(what_it_is){
|
|
|
+ .a => std.debug.print("Ant alive is: {}. ", .{insect.still_alive}),
|
|
|
+ .b => std.debug.print("Bee visited {} flowers. ", .{insect.flowers_visited}),
|
|
|
+ }
|
|
|
}
|
|
|
+
|