-
-
Save emekoi/9f51a69f5f4409c95bcd95992bbd1ec6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const E = error(union) { | |
StringError: []const u8, | |
IntError: i32, | |
VoidError, | |
}; | |
fn example(input: isize) !void { | |
if (input < 0) { | |
return error { .StringError = "input less than 0" }; | |
} | |
// do something useful | |
var value = // some boolean value | |
if (value) { | |
return error.VoidError; | |
} | |
} | |
fn usage() void { | |
example(-1) catch |err| { | |
switch (err) { | |
error.StringError => |string| // do something useful, | |
error.IntError => |int| // do something useful, | |
error.VoidError => // do something useful, | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment