Choices are a way to define a set of possible values. The Alpha compiler supports unit variants and record-body payload variants.
You create and access choices using a double colon.
# Status :: Ready, Busy, Offline;
current Status = Status::Busy
pass_status |status Status| -> Status:
return status
;
selected = pass_status(current)
Choices work with assignment and pattern matching.
# Status :: Ready, Busy;
current ~= Status::Ready
current = Status::Busy
label ~= "unset"
if current is:
case Ready => label = "ready"
case Busy => label = "busy"
;
Payload variants must use record-body syntax.
Response ::
Success,
Err |
message String,
|,
Pending |
retry_count Int,
message String,
|,
;
-- Unit variant construction
ok = Response::Success
-- Payload variant construction
error = Response::Err("bad")
pending = Response::Pending(3, message = "waiting")
Payload fields are accessed only through pattern matching. Captures use the declared field names, and they may be renamed with `as` for the arm-local binding.
if response is:
case Success => io("done")
case Err(message) => io(message)
case Pending(retry_count, message as pending_message) => io(pending_message)
;
Unit variants are values, not constructor calls.
-- Invalid: empty constructor call on a unit variant
bad = Response::Success()
Payload shorthand is not supported.
-- Invalid: shorthand payload declaration
Response :: Err String, Success;