221V 13 hours ago
parent
commit
2cf910c651
1 changed files with 21 additions and 18 deletions
  1. 21 18
      exercises/057_unions3.zig

+ 21 - 18
exercises/057_unions3.zig

@@ -15,28 +15,30 @@
 //
 const std = @import("std");
 
-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 = 17 };
-
-    std.debug.print("Insect report! ", .{});
+const InsectStat = enum{ flowers_visited, still_alive };
 
-    printInsect(ant);
-    printInsect(bee);
+const Insect = union(InsectStat){
+  flowers_visited: u16,
+  still_alive: bool,
+};
 
-    std.debug.print("\n", .{});
+pub fn main() void{
+  const ant = Insect{ .still_alive = true };
+  const 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}),
-    }
+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
@@ -52,3 +54,4 @@ fn printInsect(insect: Insect) void {
 // recently becoming mainstream, particularly in system-level
 // programming languages. You might have also seen them called
 // "variants", "sum types", or even "enums"!
+