Last active
March 22, 2020 00:49
-
-
Save halida/6fa6d3d09982e1fde5e15e5e9e50b4f6 to your computer and use it in GitHub Desktop.
union type for exception
This file contains hidden or 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
// 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