Add this to your application's shard.yml
:
dependencies:
result:
github: anicholson/result.cr
require "result/result"
struct Person
property first_name : String, last_name : String
def initialize(@first_name, @last_name)
end
end
alias PersonResult = Result(Person,String)
# wrapping values:
e = PersonResult.error("will cascade through")
v = PersonResult.ok(Person.new(first_name: "Erica", last_name: "Strange"))
e.is_a?(PersonResult) # => true
v.is_a?(PersonResult) # => true
# querying success
e.ok? # => false
v.ok? # => true
e.error? # => true
v.error? # => false
# unwrapping the Result
e.error # => "will cascade through" : String
v.value # => Person(@first_name:"Erica", @last_name:"Strange")
e.value # => raises ResultError
v.error # => raises ResultError
# chaining results
e.andThen {|r| v }.ok? # => false
e.orElse {|r| v }.ok? # => true
new_v = v.andThen {|r| r.first_name = "Tom"; PersonResult.ok(r) }
.andThen {|r| r.last_name = "Collins"; PersonResult.ok(r) }
new_v.ok? # => true
new_v.value # => Person(@first_name: "Tom", @last_name: "Collins")
v.andThen {|r| e }.ok? # => false
v.andThen {|r| e } == e # => true
- Fork it ( https://github.com/anicholson/result.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
- anicholson Andy Nicholson - creator, maintainer