Something went wrong. Try again.
A programming language for the TRS-80 Color Computer
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364# 06: No null -- Option and Result as the absence/failure story.## Exercises: generic sum types from the prelude, `<T>` instantiation,# early return, `?` propagation, `if let`-style destructuring.
# These live in the prelude but are ordinary Dodo definitions:## type Option<T> = None | Some(T)# type Result<T, E> = Ok(T) | Err(E)
const TABLE_SIZE: Uint8 = 16
type Entry = { key: Uint8, value: Uint16 }
type LookupError = | NotFound | TableCorrupt
fn find(table: [Entry; 16], key: Uint8) -> Option<Uint16> { for entry in table { if entry.key == key { # `return` for early exit; the final expression of a # block is otherwise its value, ML-style. return Some(entry.value) } } None}
fn lookup(table: [Entry; 16], key: Uint8) -> Result<Uint16, LookupError> { # `match` bridging Option to Result. A `.ok_or(...)` prelude # helper would collapse this; undecided whether methods on # prelude types are day-one. match find(table, key) { Some(value) => Ok(value), None => Err(NotFound), }}
fn lookup_pair( table: [Entry; 16], a: Uint8, b: Uint8,) -> Result<Uint16, LookupError> { # `?` unwraps Ok or early-returns the Err, exactly as in Rust. # Cheap to compile (a branch), huge ergonomic win with no-null. let first = lookup(table, a)? let second = lookup(table, b)? Ok(first + second)}
fn main() { var table: [Entry; 16] = [Entry { key: 0, value: 0 }; 16] table[3] = Entry { key: 7, value: 1000 }
# `if let` for one-armed matches. if let Some(value) = find(table, 7) { # (use value) let _ = value }
loop {}}