feat(types): option (some/none) safety type (#53)
Types phase 3 — the option/result safety pair. result/ok/err/try shipped in #46; this adds its companion option: - some(v) -> option (present value; any i32-width scalar) - none() -> option (empty; no magic -1 sentinel) - is_some / is_none -> bool - unwrap_or(o, fallback) -> int A heap %Option = { i32 present, i32 value }, bare builtins guarded by find_fn (a user fn of the same name still wins), gated by g_uses_option so unused programs compile byte-identically — same idiom as result. Wired: emit_call codegen + %Option decl (emit_decl) + g_uses_option (emit_core), reseeded seed, vocabulary sync (header/JetBrains/TextMate), builtin docs + inventory, and a self-asserting example (examples/library/optionresult.ludic + feat_case). All suites green incl. golden renders byte-identical and the bootstrap fixpoint. Tagged-union enums (variant payloads + binding match + exhaustiveness) are the deep type-system feature, split out to #56. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
5dc8394f22
commit
790eda6f73
17 changed files with 5526 additions and 4905 deletions
39
examples/library/optionresult.ludic
Normal file
39
examples/library/optionresult.ludic
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
# optionresult.ludic — the safety value types: `option` (some/none, "maybe a
|
||||
# value") and `result` (ok/err/try, "a value or a failure"). Each assertion that
|
||||
# holds prints its number, so a full run prints:
|
||||
# 1 2 3 4 5 6 7 8 9 10 11 12
|
||||
# option is issue #53; result shipped in #46. Both are additive — a program that
|
||||
# does not use them compiles byte-identically.
|
||||
program OptionResult {
|
||||
# a lookup that may find nothing — no magic -1 sentinel
|
||||
function slot_of(id: int) -> option {
|
||||
if id < 0 { return none() }
|
||||
return some(id * 10)
|
||||
}
|
||||
# a fallible parse — a value or a message
|
||||
function to_digit(c: int) -> result {
|
||||
if c >= 48 and c <= 57 { return ok(c - 48) } # '0'..'9'
|
||||
return err("not a digit")
|
||||
}
|
||||
entry {
|
||||
# --- option ---
|
||||
let a = some(42)
|
||||
if is_some(a) { print(1) }
|
||||
if not is_none(a) { print(2) }
|
||||
if unwrap_or(a, 0 - 1) == 42 { print(3) }
|
||||
let b = none()
|
||||
if is_none(b) { print(4) }
|
||||
if unwrap_or(b, 0 - 1) == 0 - 1 { print(5) }
|
||||
if unwrap_or(slot_of(5), 0) == 50 { print(6) }
|
||||
if is_none(slot_of(0 - 1)) { print(7) }
|
||||
|
||||
# --- result + try/else ---
|
||||
if is_ok(to_digit(55)) { print(8) }
|
||||
if is_err(to_digit(120)) { print(9) }
|
||||
let d = try to_digit(55) else { 0 } # '7' -> 7
|
||||
if d == 7 { print(10) }
|
||||
let bad = try to_digit(120) else { 0 - 1 } # not a digit -> fallback
|
||||
if bad == 0 - 1 { print(11) }
|
||||
let msg = try to_digit(120) else { print(12); 0 } # else binds `error`
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue