Skip to main content

Option

cameligo

val unopt : 'a option -> 'a

jsligo

let unopt: option<'a> => 'a

Returns the value if it is wrapped in the Some constructor

This function fails if the value is None.

cameligo

let v_opt : int option = Some 1

let v : int = Option.unopt v_opt (* 1 *)

let none : int = Option.unopt (None : int option) (* fails with "option is None" *)

jsligo

let v_opt : option<int> = Some(1);

let v : int = Option.unopt (v_opt); /* 1 */

let none : int = Option.unopt (None() as option<int>); /* fails with "option is None" */

cameligo

val unopt_with_error : 'a option -> string -> 'a

jsligo

let unopt_with_error: (value : option<'a>, error_msg : string) => 'a

Returns the value if it is wrapped in the Some constructor

This function fails with the provided message if the value is None.

cameligo

let v_opt : int option = Some 1

let v : int = Option.unopt_with_error v_opt "FooBar" (* 1 *)

let none : int = Option.unopt_with_error (None : int option) "FooBar" (* fails with "FooBar" *)

jsligo

let v_opt : option<int> = Some(1);

let v : int = Option.unopt_with_error (v_opt, "FooBar"); /* 1 */

let none : int = Option.unopt_with_error (None() as option<int>, "FooBar"); /* fails with "FooBar" */

cameligo

val value : 'a -> 'a option -> 'a

jsligo

let value: (default: 'a, value : option<'a>) => 'a

Returns the value if the second argument is wrapped in the Some constructor, or returns the first argument if it is None.

cameligo

val value_exn : 'e -> 'a option -> 'a

jsligo

let value_exn: (err: 'e, value : option<'a>) => 'a

Returns the value if the second argument is wrapped in the Some constructor, or fails with the first value if it is None.

cameligo

val map : ('a -> 'b) -> 'a option -> 'b option

jsligo

let map: (f : ((item: 'a) => 'b), value : option<'a>) => option<'b>

Applies the mapper function to the value if it is wrapped in the Some constructor.

If the value is None, the function is not called.

cameligo

let v : int option = Some 1

let foo (_ : int) : string = "foo"

let foo_option : string option = Option.map foo v (* Some "foo" *)

let none : string option = Option.map foo (None : int option) (* None *)

jsligo

let v : option<int> = Some(1);

let foo = (_ : int) : string => "foo";

let foo_option : option<string> = Option.map (foo, v); /* Some "foo" */

let none : option<string> = Option.map (foo, None() as option<int>); /* None */

cameligo

val is_none : 'a option -> bool

jsligo

let is_none: option<'a> => bool

Returns a boolean signaling if the value is None.

cameligo

val is_some : 'a option -> bool

jsligo

let is_some: option<'a> => bool

Returns a boolean signaling if the value is a Some.