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>
28 lines
1 KiB
Markdown
28 lines
1 KiB
Markdown
---
|
|
id: fn-some
|
|
name: some
|
|
category: builtins
|
|
kind: builtin
|
|
tokens: some
|
|
sig: some(v) -> option
|
|
tip: Wrap a present value in an option.
|
|
order: 16
|
|
---
|
|
|
|
Wraps a present value in an <code>option</code> — the value type that models <em>maybe a value</em> without a magic <code>-1</code> sentinel. An <code>option</code> is either <code>some(value)</code> (this) or <a href="fn-none"><code>none</code></a> (empty); test it with <a href="fn-is_some"><code>is_some</code></a> / <a href="fn-is_none"><code>is_none</code></a> and read it with <a href="fn-unwrap_or"><code>unwrap_or</code></a>. The payload is any <code>i32</code>-width scalar — <code>int</code>, <code>fixed</code>, <code>bool</code>, or <code>entity</code>.
|
|
|
|
Parameters:
|
|
- `v` — the present value
|
|
|
|
```ludic
|
|
program Targeting {
|
|
function nearest(count: int) -> option {
|
|
if count == 0 { return none() }
|
|
return some(count - 1) # the nearest target index
|
|
}
|
|
test "some carries the value" {
|
|
let t = unwrap_or(nearest(3), 0 - 1)
|
|
expect_eq(t, 2)
|
|
}
|
|
}
|
|
```
|