221V 1 week ago
parent
commit
7e8af8d1e9
1 changed files with 41 additions and 34 deletions
  1. 41 34
      exercises/024_errors4.zig

+ 41 - 34
exercises/024_errors4.zig

@@ -14,18 +14,18 @@
 const std = @import("std");
 
 const MyNumberError = error{
-    TooSmall,
-    TooBig,
+  TooSmall,
+  TooBig,
 };
 
-pub fn main() void {
-    // The "catch 0" below is a temporary hack to deal with
-    // makeJustRight()'s returned error union (for now).
-    const a: u32 = makeJustRight(44) catch 0;
-    const b: u32 = makeJustRight(14) catch 0;
-    const c: u32 = makeJustRight(4) catch 0;
-
-    std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
+pub fn main() void{
+  // The "catch 0" below is a temporary hack to deal with
+  // makeJustRight()'s returned error union (for now).
+  const a: u32 = makeJustRight(44) catch 0;
+  const b: u32 = makeJustRight(14) catch 0;
+  const c: u32 = makeJustRight(4) catch 0;
+  
+  std.debug.print("a={}, b={}, c={}\n", .{ a, b, c });
 }
 
 // In this silly example we've split the responsibility of making
@@ -36,34 +36,41 @@ pub fn main() void {
 //     fixTooSmall()     Calls detectProblems(), fixes TooSmall errors.
 //     detectProblems()  Returns the number or an error.
 //
-fn makeJustRight(n: u32) MyNumberError!u32 {
-    return fixTooBig(n) catch |err| {
-        return err;
-    };
+fn makeJustRight(n: u32) MyNumberError!u32{
+  return fixTooBig(n) catch |err|{
+    return err;
+  };
 }
 
-fn fixTooBig(n: u32) MyNumberError!u32 {
-    return fixTooSmall(n) catch |err| {
-        if (err == MyNumberError.TooBig) {
-            return 20;
-        }
-
-        return err;
-    };
+fn fixTooBig(n: u32) MyNumberError!u32{
+  return fixTooSmall(n) catch |err|{
+    if(err == MyNumberError.TooBig){
+      return 20;
+    }
+    
+    return err;
+  };
 }
 
-fn fixTooSmall(n: u32) MyNumberError!u32 {
-    // Oh dear, this is missing a lot! But don't worry, it's nearly
-    // identical to fixTooBig() above.
-    //
-    // If we get a TooSmall error, we should return 10.
-    // If we get any other error, we should return that error.
-    // Otherwise, we return the u32 number.
-    return detectProblems(n) ???;
+fn fixTooSmall(n: u32) MyNumberError!u32{
+  // Oh dear, this is missing a lot! But don't worry, it's nearly
+  // identical to fixTooBig() above.
+  //
+  // If we get a TooSmall error, we should return 10.
+  // If we get any other error, we should return that error.
+  // Otherwise, we return the u32 number.
+  return detectProblems(n) catch |err|{
+    if(err == MyNumberError.TooSmall){
+      return 10;
+    }
+    
+    return err;
+  };
 }
 
-fn detectProblems(n: u32) MyNumberError!u32 {
-    if (n < 10) return MyNumberError.TooSmall;
-    if (n > 20) return MyNumberError.TooBig;
-    return n;
+fn detectProblems(n: u32) MyNumberError!u32{
+  if(n < 10){ return MyNumberError.TooSmall; }
+  if(n > 20){ return MyNumberError.TooBig; }
+  return n;
 }
+