Skip to content

Stdlib: add `is_none`, `is_some`, `value` and `value_exn` in module `Option`

E. Rivas requested to merge er433/stdlib/is_none into dev

Motivation and Context

For certain case reported by Benjamin in #ligo-dev, it'd be beneficial to have an Option.is_none in the stdlib.

Description

This MR adds Option.is_none, Option.is_some, Option.value and Option.value_exn to the stdlib.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Performance improvement (non-breaking change that improves performance)
  • None (change with no changelog)

Changelog

New functions available in module Option:

  • val is_none : 'a option -> bool: returns a boolean signaling if the value is None.
  • val is_some : 'a option -> bool: returns a boolean signaling if the value is a Some.
  • val value : 'a -> 'a option -> 'a: returns the value if the second argument is wrapped in the Some constructor, or returns the first argument if it is None.
  • val value_exn : 'e -> 'a option -> 'a: returns the value if the second argument is wrapped in the Some constructor, or fails with the first value if it is None.

Example

type foobar = option <int>;

const s : foobar = Some (42);
const n : foobar = None ();

const f = (m : foobar) : int => {
  if (Option.is_none(m)) {
    return Option.value(1, m);
  } else {
    if (Option.is_some(m)) {
      return Option.value_exn("won't happen", m);
    } else {
      return -1;
    }
  }
}

const j = f(s);
const k = f(n);

j evaluates to 42, while k evaluates to 1.

Checklist:

  • Changes follow the existing coding style (use dune @fmt to check).
  • Tests for the changes have been added (for bug fixes / feature).
  • Documentation has been updated.
  • Changelog description has been added (if appropriate).
  • Start titles under ## Changelog section with #### (if appropriate).
  • There is no image or uploaded file in changelog
  • Examples in changed behaviour have been added to the changelog (for breaking change / feature).
Edited by E. Rivas

Merge request reports