Try monad for Crystal.
require "try"
i = Try(Int32).try { 1 } # => Success(Int32)
i.get # => 1
i.err # raises "not failed"
i.value # => 1
i.map(&.+ 1).value # => 2
i = Try(Int32).try { raise "error" } # => Failure(Int32)
i.get # raises "error"
i.err # => Exception("error")
i.value # => Exception("error")
i.map(&.+ 1).value # => Exception("error")
def success? : Bool
def failure? : Bool
def value : T
def get : T
def get? : T?
def failed : Failure(T)
def err : Exception
def err? : Exception?
def map(&block : T -> U) : Failure(T) | Success(T) forall U
def flat_map(&block : T -> Failure(T) | Success(T)) : Failure(T) | Success(T) forall U
def recover(&block : Exception -> T) : Failure(T) | Success(T)
def or(&block : Exception -> U) : Failure(T|U) | Success(T|U) forall U
Syntax sugar for case
statements that specialize in getting Success
and Failure
values like extractors in Scala.
try = Try(Int32).try { 1 }
Try.match(try) {
v : Success = puts "got #{v}"
e : Failure = raise e
}
Add this to your application's shard.yml
:
dependencies:
try:
github: maiha/try.cr
version: 1.6.0
make ci
- Fork it ( https://github.com/maiha/try.cr/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- maiha maiha - creator, maintainer