ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • zig연습)zig 에러4 마지막에 막힘
    코딩Coding/Zig_❤️lang_연습 2022. 8. 5. 08:05
    728x90
    const std = @import("std");
    const print = std.debug.print;
    
    const MyNumberError = error{
        TooSmall,
        TooBig,
    };
    
    pub fn main() void {
        var a: u32 = makeJustRight(44) catch 0;
        var b: u32 = makeJustRight(14) catch 0;
        var c: u32 = makeJustRight(4) catch 0;
    
        print("a={}, b={}, c={}\n", .{ a, b, c });
    }
    
    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 fixTooSmall(n: u32) MyNumberError!u32 {
        return detectProblems(n);
    }
    
    fn detectProblems(n: u32) MyNumberError!u32 {
        if (n < 10) return MyNumberError.TooSmall;
        if (n > 20) return MyNumberError.TooBig;
    }

















    반응형
Designed by Tistory.