38 lines
No EOL
2.8 KiB
HTML
38 lines
No EOL
2.8 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>panic — Ludic</title>
|
||
<meta name="description" content="Abort with a located error message instead of crashing.">
|
||
<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">panic</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-builtin">function</span>
|
||
<h1 id="top">panic</h1>
|
||
</div>
|
||
<code class="sig">panic(msg: string)</code>
|
||
<div class="desc"><p>Prints <code>file:line: panic: <msg></code> to standard error and aborts the process with exit code 1. It turns "something went wrong" into a clear, located message a non-expert can act on — the opposite of a raw segfault or a silent wrong result. Use it for the unrecoverable case: a corrupt asset, a broken invariant, a branch that should be impossible. The source location is baked in at compile time, so the message always points at the exact call. For a guarded check that only aborts when a condition fails, use <a href="fn-assert"><code>assert</code></a>.</p><p>Recoverable failures — a missing save, a bad network packet — are a different story: those should be values a game can fall back from rather than a <code>panic</code>. That value-carrying path is <a href="kw-try"><code>try</code></a>/<code>else</code> over a <a href="fn-ok"><code>result</code></a>; <code>panic</code> covers the unrecoverable programmer-bug half.</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 what went wrong (a string)</span></div></div>
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Demo {
|
||
function pick(options: int, n: int) -> int {
|
||
if n < 0 { panic("choice index is negative") }
|
||
if n >= options { panic("choice index out of range") }
|
||
return n
|
||
}
|
||
entry {
|
||
print(pick(3, 1)) # 1
|
||
print(pick(3, 9)) # aborts: Demo.ludic:6: panic: choice index out of range
|
||
}
|
||
}</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> |