Skip to content

Instantly share code, notes, and snippets.

@emekoi
Last active May 25, 2019 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emekoi/9f51a69f5f4409c95bcd95992bbd1ec6 to your computer and use it in GitHub Desktop.
Save emekoi/9f51a69f5f4409c95bcd95992bbd1ec6 to your computer and use it in GitHub Desktop.
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