37 lines
No EOL
2.5 KiB
HTML
37 lines
No EOL
2.5 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>err — Ludic</title>
|
||
<meta name="description" content="Wrap a failure message in a result — the sad half, recovered by try/else.">
|
||
<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#builtins">Builtin functions</a> <span>›</span> <span class="here">err</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-builtin">function</span>
|
||
<h1 id="top">err</h1>
|
||
</div>
|
||
<code class="sig">err(msg: string) -> result</code>
|
||
<div class="desc"><p>Wraps a failure in a <code>result</code>, carrying a message that says what went wrong. It is the counterpart to <a href="fn-ok"><code>ok</code></a>: a fallible function (declared <code>-> result</code>) returns <code>err("…")</code> on the sad path instead of crashing or returning a magic sentinel. The caller recovers with <a href="kw-try"><code>try</code></a>/<code>else</code> — the <code>else</code> block sees the message as <code>error</code> — or tests it with <a href="fn-is_err"><code>is_err</code></a>. Unlike <a href="fn-panic"><code>panic</code></a>, which aborts, <code>err</code> is a value: the program keeps running and chooses a fallback.</p></div>
|
||
<div class="params"><h2>Parameters</h2><div class="param" id="param-msg"><code class="pname">msg</code><span class="pdesc">a message describing the failure (a string)</span></div></div>
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Config {
|
||
function volume(pct: int) -> result {
|
||
if pct < 0 or pct > 100 { return err("volume out of range") }
|
||
return ok(pct)
|
||
}
|
||
test "err drives the fallback" {
|
||
let v = try volume(150) else { 100 } # clamp to a safe default
|
||
expect_eq(v, 100)
|
||
}
|
||
}</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> |