lloyAlloy on GitHub
Macros
Docs

Macros

Intrinsics and macros with a $ sigil

Updated

Intrinsics see source text and position. A macro is a compile-time template with expression parameters. Its declaration leaves blank lines, and each call expands in place.

Alloymacros.aly
1macro clamp01(x)
2    math.clamp(x, 0, 1)
3end
4local ratio = $clamp01(speed / 16)
5$assert_eq(ratio, 1)
6local field = $nameof(RunService.Heartbeat)
Emitted Luausame line count
1local __alloy = require("@alloy")
2
3
4local ratio = math.clamp((speed / 16), 0, 1)
5__alloy.assert_eq("macros.aly:5", "ratio", ratio, "1", 1)
6local field = "Heartbeat" return nil
  • The set is closed: $dbg, $todo, $unreachable, $assert, $assert_eq, $expect, $nameof, $stringify, $set, $map, $matches, $bnot.
  • A macro body sees globals and its parameters, never a module local, since it expands in the calling file.
  • $ opens completion with every intrinsic and every macro in the workspace; a macro hovers with its header.