Codectionary / Developer documentation / Luau

Luau recursive record types

Recursive type aliases describe linked structures such as trees and lists. An optional link supplies a stopping point; recursion in a type does not allocate any runtime data.

Syntax

type Node = {value: number, next: Node?}

Examples

Luau recursive record types

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 Node = {value: number, next: Node?}
local head: Node = {value = 3, next = {value = 5, next = nil}}
local cursor: Node? = head
local total = 0
while cursor ~= nil do
    total += cursor.value
    cursor = cursor.next
end
print(total)
  1. Output: 8. Each iteration follows a link until the optional next field is nil.

Best practices

  • Include an explicit terminating case. Cycles are possible at runtime and require a visited set or another stopping policy.

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

Specifications & further reading