Skip to content

Instantly share code, notes, and snippets.

@halida
Last active March 22, 2020 00:49
Show Gist options
  • Save halida/6fa6d3d09982e1fde5e15e5e9e50b4f6 to your computer and use it in GitHub Desktop.
Save halida/6fa6d3d09982e1fde5e15e5e9e50b4f6 to your computer and use it in GitHub Desktop.
union type for exception
// File API
get_file(string filename) -> (File | NotFoundException) { ... }
close_file(File file) -> void { ... }
read_file(File file) -> (string | IOFoundException) { ... }
// File helper
<T>with_file(filename, Func<file, T> func) -> (T | NotFoundException) {
var file = get_file(filename);
if (file is NotFoundException) { return result; }
var result = func(file);
close_file(file);
return result;
}
// usage
get_content_from_file(filename) -> (string | NotFoundException | IOFoundException ) {
return with_file(filename, (file)-> {
return read_file(file);
});
}
skip_check_exception(filename) -> (void | Exception) {
var result = get_content_from_file(filename);
if (result is Exception) { return (Exception)result; }
// keep process result
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment