Syntax
type Result = {ok: true, value: number} | {ok: false, message: string}Examples
Luau tagged result unions
Run as a standalone current Luau script, or as a Script in Roblox Studio ServerScriptService. No Roblox engine APIs are required; type-annotated syntax is not standard Lua.
--!strict
type Result = {ok: true, value: number} | {ok: false, message: string}
local function parse(text: string): Result
local n = tonumber(text)
if n == nil then return {ok = false, message = "invalid"} end
return {ok = true, value = n}
end
local result = parse("21")
if result.ok then
print(result.value * 2)
else
print(result.message)
end- Output: 42. Passing non-numeric text prints invalid through the error branch.
Best practices
- Use distinct literal tags and refine before reading branch-specific fields. The type checker does not validate network payloads.
Runtime & versions
Luau host; Roblox engine examples require Roblox Studio. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
Luau documentation