Result
Result[T] is ok(value) or error(...), which holds an Error.
ok #
ok(value: T) -> Result[T]
ok() -> Result[Void]def main() -> Void
puts(ok(3))
puts(ok("yes"))
endOutput
ok(3)
ok("yes")error #
error(message: String) -> Result[T]
error(message: String, kind: String, code: Int, details: Map[String, String], cause: Error) -> Result[T]
error(error: Error) -> Result[T]With a String, it builds a new Error with that message, made at this call. The other fields come as named arguments, each optional and in any order; code and cause are stored as some(...) (Error). With an Error, it holds that same Error, so a case arm can pass an error on as a Result of another type. The type comes from an annotation, a return type, a parameter or a field.
def fail_now() -> Result[Int] = error("broken")
def main() -> Void
puts(fail_now())
endOutput
error("broken")Result.ok? #
Result.ok?(result: Result[T]) -> Booldef main() -> Void
puts("1".to_i.ok?)
puts("one".to_i.ok?)
endOutput
true
falseResult.unwrap! #
Result.unwrap!(result: Result[T]) -> TGives the value, or stops the program with RUNTIME-UNWRAP-ERROR.
def main() -> Void
puts("5".to_i.unwrap! * 2)
endOutput
10Result.unwrap_or #
Result.unwrap_or(result: Result[T], default: T) -> TGives the value, or the default for an error. The default is always computed.
def main() -> Void
puts("5".to_i.unwrap_or(0))
puts("five".to_i.unwrap_or(0))
endOutput
5
0Result.message #
Result.message(result: Result[T]) -> StringGives the message of the error, or "" for a success.
def main() -> Void
puts("five".to_i.message)
puts("[" + "5".to_i.message + "]")
endOutput
cannot convert "five" to Int
[]