ludic/fn-assert.html
2026-09-17 22:10:03 +00:00

38 lines
No EOL
2.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>assert — Ludic</title>
<meta name="description" content="Abort with a located message when an invariant is false.">
<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">assert</span></div>
<div class="item-head">
<span class="kind-badge kind-builtin">function</span>
<h1 id="top">assert</h1>
</div>
<code class="sig">assert(cond: bool, msg: string)</code>
<div class="desc"><p>When <code>cond</code> is false, prints <code>file:line: assertion failed: &lt;msg&gt;</code> to standard error and aborts (exit code 1); when it is true, execution continues. It is the guarded form of <a href="fn-panic"><code>panic</code></a> — for the programmer-bug cases the language should catch loudly rather than let corrupt the world: an index that must be in range, a value that must be non-negative, a state that must hold before a step. The location is baked in at compile time, so a failure points at the exact assertion. Assertions document and enforce the invariants a system relies on; keep them for "this must be true" checks, not for recoverable runtime conditions.</p></div>
<div class="params"><h2>Parameters</h2><div class="param" id="param-cond"><code class="pname">cond</code><span class="pdesc">the invariant that must hold (a bool)</span></div><div class="param" id="param-msg"><code class="pname">msg</code><span class="pdesc">a message describing the invariant (a string)</span></div></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Demo {
function withdraw(balance: int, amount: int) -&gt; int {
assert(amount &gt;= 0, "amount must be non-negative")
assert(amount &lt;= balance, "cannot overdraw")
return balance - amount
}
entry {
print(withdraw(100, 30)) # 70
print(withdraw(100, 250)) # aborts: Demo.ludic:4: assertion failed: cannot overdraw
}
}</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>