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>ok — Ludic</title>
|
||
<meta name="description" content="Wrap a success payload in a result — the happy half of 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">ok</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-builtin">function</span>
|
||
<h1 id="top">ok</h1>
|
||
</div>
|
||
<code class="sig">ok(v) -> result</code>
|
||
<div class="desc"><p>Wraps a success value in a <code>result</code> — the value type a fallible function returns. A <code>result</code> carries either a success payload (this) or a failure message (from <a href="fn-err"><code>err</code></a>), and a caller recovers it with <a href="kw-try"><code>try</code></a>/<code>else</code> or classifies it with <a href="fn-is_ok"><code>is_ok</code></a> / <a href="fn-is_err"><code>is_err</code></a>. A function that can fail declares <code>-> result</code> and returns <code>ok(value)</code> on the happy path.</p><p>The payload is any <code>i32</code>-width scalar — <code>int</code>, <code>fixed</code>, <code>bool</code>, or <code>entity</code>.</p></div>
|
||
<div class="params"><h2>Parameters</h2><div class="param" id="param-v"><code class="pname">v</code><span class="pdesc">the success payload</span></div></div>
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Parse {
|
||
function to_digit(c: int) -> result {
|
||
if c >= 48 and c <= 57 { return ok(c - 48) } # '0'..'9'
|
||
return err("not a digit")
|
||
}
|
||
test "ok carries the parsed value" {
|
||
let d = try to_digit(55) else { 0 } # '7' -> 7
|
||
expect_eq(d, 7)
|
||
}
|
||
}</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> |