Language / Optionals (T?)

Optionals (T?)

Optionals represent “maybe a value” without sentinel nulls.

  • The nominal form is Option(T).
  • The idiomatic form is suffix T?.
  • Values are None (empty) or Some(value) (present).
  • Use ?? (coalescing), ?. (optional field access), and match to consume optionals.

Full reference: optional.

Notes#

  • Supported forms + backend payload limits: optional

Syntax#

let a: int? = None;
let b: int? = Some(123);

let x: int = b ?? 0;
let y: int = match b {
  None => 0,
  Some(v) => v,
};

Examples#

Example: ?? and match#

fn main () -> int {
  let x: int? = Some(7);
  let y: int = x ?? 0;
  let z: int = match x {
    None => 1,
    Some(v) => v,
  };
  return y + z;
}

Example: optional struct field access with ?.#

struct Profile {
  email: string,
}

struct User {
  profile: Profile?,
}

fn main () -> int {
  let u: User = User{ profile: Some(Profile{ email: "a@b" }) };
  let email: string = u.profile?.email ?? "no-email";
  if email == "a@b" {
    return 0;
  }
  return 1;
}

See also#

Source repository · Edit this page · View Markdown