Result is a container type for value that might be failed with error.
// Creation.
let error = Error "Something wrong"
let ok = Ok 42
// Decomposition.
match ok with
| Ok n -> assert (n = 42)
| Error _ -> assert false
// Pipeline.
result |> Result.bind fallibleFun |> Result.map pureFun
Result<T, E>
is a result type whose value type is T
and error type is E
.
A computation that can abort with an error returns a value of result type.
WIP: add intuitive example where there are two kind of non-logical failure
Result is similar to option type.
Prefer Result over option when reason of failure is concerned.
Milone-lang doesn't support exceptions.
- Use assertion or "abort with message" for logical or non-recoverable error.
- One might attempt to write perfectly precise error type, which might be unnecessary.
- A function is fallible when its result type is
Result
.
Result is a generic discriminated union type.
- See definition in
Std.Result
- See details in discriminated union types#Runtime Representation
Name of Result
type starts with uppercase unlike option
.
- Option type originates in OCaml language (and its ancestors) where type name should start with lowercase.
- Result type is introduced in F# 4.1in 2017-02. At that time, OCaml didn't have result type.
- After that,
result
type (lowercase) is added to OCaml 4.08 in 2019-06.