Skip to content

Option and Result combinators #211

@aallan

Description

@aallan

Add standard combinator functions for Option and Result types. Currently every Option/Result value requires a full match expression, which is verbose when chaining multiple fallible operations.

Proposed functions

Option combinators

  • map_option(forall<A, B> @Option<A>, fn(A -> B) effects(<E>) -> @Option<B>) effects(<E>)
  • unwrap_or(forall<T> @Option<T>, @T -> @T)
  • and_then(forall<A, B> @Option<A>, fn(A -> Option<B>) effects(<E>) -> @Option<B>) effects(<E>)

Result combinators

  • map_result(forall<A, B, E> @Result<A, E>, fn(A -> B) effects(<F>) -> @Result<B, E>) effects(<F>)
  • unwrap_or_result(forall<T, E> @Result<T, E>, @T -> @T)

Design notes

The spec already shows map_option as an example of effect-polymorphic functions (spec/07-effects.md:250, spec/05-functions.md:325). These can technically be written as user functions today, but they are so fundamental they belong in a standard prelude.

Fully generic versions may benefit from #60 (abilities) for type constraint syntax.

Dependencies

Metadata

Metadata

Assignees

No one assigned

    Labels

    C9C9 — Language designdesignFuture language design (spec §0.8)enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions