@derive(Clone)
@derive(Clone)
struct Bag
items: string[]
end
local copy = new Bag { items = [ "rope" ] }:clone()Generates clone: a new value with the same metatable, field by field, the way Rust's derive clones. A field whose type is a struct that derives Clone, in this file or imported, clones through its own clone, and so does each element of an array of one. An array, a table, a HashMap, or a Set field becomes a new one with the same entries. Any other value, such as an Instance, is shared.
@derive(Debug)
@derive(Debug)
struct Point
x: number
y: number
end
print(new Point { x = 1, y = 2 }:debug())Generates debug and __tostring: the struct's name and its fields, as text. Every struct prints that way by default; the derive adds the debug method, and a to_string in the impl replaces both.
@derive(Default)
@derive(Default)
struct Stats
hp: number
level: number = 1
tags: string[]
end
local s = Stats.default()Generates default(), a value with every field at its starting value. A field that declares a default keeps it. The rest start at the zero of their type: 0, "", false, nil for T?, an empty array, map, or set, Vector3.zero, Vector2.zero, CFrame.identity, and new() of UDim, UDim2, and Color3. A field whose type is a struct that derives Default, in this file or imported, starts as that struct's default(). A field with no zero, an Instance or a record, reports; give it a default.
@derive(Deserialize)
import { Deserialize } from "@alloy/std/serde"
@derive(Deserialize)
struct Save
coins: number
end
local save = Save.from_table({ coins = 10 })Generates from_table, which builds the struct from the plain table Serialize writes, metatable and defaults included. A struct that round-trips derives both.
A field whose type is a struct that also derives Deserialize, in this file or imported, reads back through that struct's own from_table, and so does each struct in an array or a map. An array comes back as an Array, a HashMap and a Set are rebuilt, and a payload enum gets its metatable back, so the methods work on a value loaded from a DataStore. An enum field takes a variant alone: a name or a tag no variant has raises and names the struct, the field and the value, so an old save's "Uncommon" never reads as another rarity. A key the table lacks leaves the field to its default, so an older save loads. @rename and @skip read the same keys Serialize writes.
The name comes from @alloy/std/serde, so a project under [std] globals = "none" imports it.
@derive(Eq)
@derive(Eq)
struct Point
x: number
y: number
end
print(new Point { x = 1, y = 2 } == new Point { x = 1, y = 2 })Generates __eq: two values are equal when every field is. A field that holds a table, an array, a map, or a set compares by its contents, as Rust compares a Vec.
@derive(Ord)
@derive(Ord)
struct Version
major: number
minor: number
end
print(new Version { major = 1, minor = 2 } < new Version { major = 2, minor = 0 })Generates __lt and __le: the fields compare in declaration order, and the first that differs decides, as a tuple compares. <, <=, >, >=, and table.sort then work on the values.
@derive(PartialEq)
@derive(PartialEq)
struct Point
x: number
y: number
endThe same __eq as Eq, under the name Rust uses. Luau has one equality, so the two derive the same method.
@derive(Serialize)
import { Serialize } from "@alloy/std/serde"
@derive(Serialize)
struct Save
coins: number
end
local data = new Save { coins = 10 }:to_table()Generates to_table and serialize, the plain table for storage and remotes. serialize calls to_table, so the struct meets a T: Serialize bound. Deserialize reads the table back.
A field whose type is a struct that also derives Serialize, in this file or imported, goes through that struct's own to_table, so the table nests. An array or a map of such structs writes each one that way, a HashMap writes its entries as a table, and a Set writes its items as an array. Two fields under one key, and a field named like a method the derive writes, report. @rename sets a field's key and @skip leaves it out.
The name comes from @alloy/std/serde, so a project under [std] globals = "none" imports it.