48 lines
No EOL
3.3 KiB
HTML
48 lines
No EOL
3.3 KiB
HTML
<!doctype html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title>match — Ludic</title>
|
||
<meta name="description" content="Multi-way branch on one value, matching one or more literals per arm.">
|
||
<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">match</span></div>
|
||
<div class="item-head">
|
||
<span class="kind-badge kind-keyword">keyword</span>
|
||
<h1 id="top">match</h1>
|
||
</div>
|
||
<code class="sig">match value { 0, 1 => … _ => … }</code>
|
||
<div class="desc"><p>A <code>match</code> replaces an <code>if</code>/<code>else</code> ladder that tests one value against several constants. It evaluates the subject once, then takes the first arm whose pattern matches; an arm lists one or more literal patterns separated by commas and points at a body with <code>=></code>, and a lone <code>_</code> arm is the catch-all default. Patterns are compile-time constants — integers, char literals like <code>'w'</code>, or <code>enum</code> variants such as <code>Action.Guard</code> — which makes <code>match</code> ideal for dispatching on a key press, a tile code, or a mode. Each arm's body is a single statement or a <code>{ … }</code> block; matching lowers to plain branches, so it is as cheap as the <code>if</code> chain it replaces.</p><p>When the subject is a **tagged-union enum** (an <code>enum</code> whose variants carry payloads), <code>match</code> also *destructures* it: an arm names a variant and binds its payload — <code>Door(n) => …</code>, <code>Portal(x, y) => …</code> — with <code>n</code>, <code>x</code>, <code>y</code> in scope for that arm's body. A tagged <code>match</code> must be **exhaustive**: it covers every variant or ends with a <code>_</code>, or the compiler rejects it, so a newly added variant flags every match that must handle it. See [enum](../structure/kw-enum) for the full picture.</p></div>
|
||
|
||
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program Steering {
|
||
property Velocity { delta_x: int = 0, delta_y: int = 0 }
|
||
model Player { Velocity }
|
||
|
||
handler Boot phase Start {
|
||
spawn Hero { Velocity { delta_x: 0, delta_y: 0 } }
|
||
}
|
||
|
||
handler ReadKeys phase Input {
|
||
let pressed = Input.key()
|
||
for (velocity) in query [Velocity, {Player}] {
|
||
match pressed {
|
||
'w' => velocity.delta_y = -1
|
||
's' => velocity.delta_y = 1
|
||
'a', 'h' => velocity.delta_x = -1
|
||
'd', 'l' => velocity.delta_x = 1
|
||
_ => velocity.delta_x = 0
|
||
}
|
||
}
|
||
}
|
||
}</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> |