51 lines
No EOL
3.2 KiB
HTML
51 lines
No EOL
3.2 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>try — Ludic</title>
|
||
<meta name="description" content="Recover a fallible result as a value, with a fallback — no exceptions, no unwinding.">
|
||
<link rel="stylesheet" href="base.css">
|
||
<link rel="stylesheet" href="docs.css">
|
||
</head>
|
||
<body>
|
||
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
|
||
<main class="wrap item">
|
||
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="api.html#control">Control flow</a> <span>›</span> <span class="here">try</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-keyword">keyword</span>
|
||
<h1 id="top">try</h1>
|
||
</div>
|
||
<code class="sig">try EXPR else { … }</code>
|
||
<div class="desc"><p>A <code>try EXPR else { … }</code> expression recovers a fallible operation without exceptions or unwinding. <code>EXPR</code> evaluates to a <a href="fn-ok"><code>result</code></a> — a value carrying either a success payload (from <a href="fn-ok"><code>ok</code></a>) or a failure message (from <a href="fn-err"><code>err</code></a>). When it is <code>ok</code>, the whole <code>try</code> expression <em>is</em> that payload and the <code>else</code> block is skipped. When it is <code>err</code>, the <code>else</code> block runs and its trailing expression supplies the fallback value; inside the block the failure message is bound to <code>error</code>, so you can log or branch on it. It is a plain branch on the result's tag — the happy path stays one line, there is no hidden control flow, and nothing unwinds the stack.</p><p>The payload is an <code>i32</code>-width scalar (<code>int</code>, <code>fixed</code>, <code>bool</code>, or <code>entity</code>) — the common case of "a number or nothing". For richer payloads and full pattern-matching over tagged unions, see the type-system work (#1).</p></div>
|
||
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program SaveLoad {
|
||
# a fallible op returns a result: ok(payload) or err(message)
|
||
function load_score(slot: int) -> result {
|
||
if slot == 1 { return ok(4200) }
|
||
return err("no save in that slot")
|
||
}
|
||
|
||
test "ok yields the payload, err falls back" {
|
||
let a = try load_score(1) else { 0 } # 4200
|
||
let b = try load_score(9) else { 0 } # 0 (fallback)
|
||
expect_eq(a, 4200)
|
||
expect_eq(b, 0)
|
||
}
|
||
|
||
test "the fallback can read the failure via `error`" {
|
||
var logged = 0
|
||
let score = try load_score(3) else {
|
||
if error == "no save in that slot" { logged = 1 }
|
||
0 # trailing expression = the fallback
|
||
}
|
||
expect_eq(score, 0)
|
||
expect_eq(logged, 1)
|
||
}
|
||
}</pre></div>
|
||
|
||
<a class="back" href="api.html">← All symbols</a>
|
||
</main>
|
||
<script src="ludic-highlight.js"></script>
|
||
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
|
||
</body></html> |