Created
June 6, 2018 11:21
-
-
Save anicholson/6a5dd95590f7faed69c0ef9021c09ed0 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
class ResultError < Exception; end | |
abstract class Result(V, E) | |
abstract def value : V | |
abstract def error : E | |
abstract def ok? : Boolean | |
abstract def error? : Boolean | |
abstract def andThen(&block : self -> self) : self | |
abstract def orElse(block : self -> self) : self | |
end | |
class Ok(V, E) < Result(V, E) | |
def initialize(@value : V) | |
end | |
getter value | |
def error | |
raise ResultError.new("Ok has no error") | |
end | |
def ok? | |
true | |
end | |
def error? | |
false | |
end | |
def andThen(&block) | |
yield self | |
end | |
def orElse(&block) | |
self | |
end | |
end | |
class Error(V, E) < Result(V, E) | |
def initialize(@error : E) | |
end | |
getter error | |
def value | |
raise ResultError.new("Error has no value") | |
end | |
def ok? | |
false | |
end | |
def error? | |
true | |
end | |
def andThen(&block) | |
self | |
end | |
def orElse(&block) | |
yield self | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment