Skip to content

Latest commit

 

History

History
63 lines (38 loc) · 1.83 KB

result_type.md

File metadata and controls

63 lines (38 loc) · 1.83 KB

Result Type

Result is a container type for value that might be failed with error.

Instant Usage

    // 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

Guide-level Explanation

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

Difference from Option Type

Result is similar to option type.

Prefer Result over option when reason of failure is concerned.

Difference from Exceptions

Milone-lang doesn't support exceptions.

Remarks

  • 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.

Advanced Topics

Wording

  • A function is fallible when its result type is Result.

Runtime Representation

Result is a generic discriminated union type.

History of Name

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.