OxL Reference

kw enum

Reserved keyword

The list of words: §1.4

Syntax

ebnf
Enum        = "enum" Ident [ Generics ] "{" Variant { ( "," | NL ) Variant } "}" ;

Grammar: App. A

Defined in

  • 4.4 enum — A variant: without fields, with positional or with named fields.
  • 9.3 The OxL wire format — The OxL binary format is protobuf 3 encoding with the following mappings
  • 9.2 Field numbers and schema — Every field of a data and every variant of an enum has a stable tag (the number in the wire format).
  • 3.4 Declared types — data — a product type with named fields (§4.2).
  • 3.9 Copy and move — Copy types: primitives, str, bytes, Duration, Time and other time types, distinct/refined types over a Copy type, tuples and arrays of Copy
  • 4.6 Interfaces — Interfaces are structural
  • 6.5 Errors — There are no exceptions.
  • 7.2 Borrows — &x creates a shared borrow, &var x an exclusive one.

Examples

oxl
pub(api) enum Role { User, Support, Admin }

pub(api) enum Shape {
  Circle(r: f64)
  Rect(w: f64, h: f64)
  Empty
}

@error enum PayError { Declined(reason: str), Fraud, Limit(max: Money) }

@open pub(api) enum Status { New = 1, Paid = 2, Shipped = 3 }

— 4.4 enum

oxl
@error data NotFound { what: str }
@error enum PayError { Declined(reason: str), Fraud }

fn charge(card: Card, sum: Money) -> Receipt | PayError | NetError  net {
  let token = Vault.tokenize(card)?          // ? propagates the errors of the call
  if sum <= Money.zero() { fail PayError.Declined("zero sum") }
  Bank.pay(token, sum)
}

— 6.5 Errors

Related diagnostics

secret-json, schema-attr, schema-break, unknown-type, use-after-move, explicit-interface, interface-not-satisfied, fail-type, question-context, unhandled-error, borrow-conflict, borrow-escape

See also