Top Level Namespace
Defined in:
Macro Summary
-
result!(value, ok = :done, err = :fail)
Takes a value and make a
Result
. - try!(result)
-
unwrap!(value)
Try to unwrap value (like
Result#unwrap
).
Macro Detail
Takes a value and make a Result
.
Ok
(ok) instance if value does not raise anException
.- or force the caller to return
Err
(err) if anException
occurred (in this case, the rest of the code isn't executed).
# With a basic *value*
def something(value : Number) : Result
# Wrap value into a `Result` instance (struct: `Ok` or `Err`).
# If an error occurred, here *something* method returns an `Err` instance.
res = result!(value)
# Executed only if `result!` (above) has returned an `Ok` instance,
pp "does something"
pp res.unwrap # => Number
res
end
macro try!(result)
#
Get the Result
value (unwrapped) or force the caller to return Err
(if Err
, the rest of the code isn't executed).
# With a `Result` instance
def something(res : Result) : Result
# Try to unwrap original value.
# If result is an error, here *something* method returns this `Err` instance.
data = try!(res)
# Executed if `try!` (above) has returned the original value.
pp "does something"
# Original value
pp data
# Wrap data into a `Result` struct:
# Returns success
# `Ok` / `Ok.status # => :done`
Ok.done data
# Or returns an error
# `Err` / `Err.status # => :fail`
Err.fail "Oops!"
end
macro unwrap!(value)
#
Try to unwrap value (like Result#unwrap
).
If value is not a Result
(Ok
or Err
), value is simply forwarded.
NOTE: To proceed, this macro checks if valueresponds_to
unwrap
method.
res = Ok.done("hello") # or Ok.new("hello")
value = unwrap!(res) # => "hello"
res = Err.fail("Oops")
value = unwrap!(res) # => raise Exception.new "Oops"
foo = "bar"
value2 = unwrap!(foo) # => "bar"