Skip to content

Instantly share code, notes, and snippets.

@anicholson
Created June 6, 2018 11:21
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 anicholson/6a5dd95590f7faed69c0ef9021c09ed0 to your computer and use it in GitHub Desktop.
Save anicholson/6a5dd95590f7faed69c0ef9021c09ed0 to your computer and use it in GitHub Desktop.
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