DiagnosticsReferenceNew · 0.744
replacetable
Replace a local table's contents while preserving its identity.
Details#
Removes the target table's existing keys, then shallow-copies the source table's entries into that same target object. Existing references to the target continue to refer to it. Nested tables and other reference values are shared with the source rather than cloned.
Signature#
replacetable(target: table, source: table): tableParameters#
| Parameter | Type | Description |
|---|---|---|
target | table | Mutable local table whose entries will be replaced. |
source | table | Local table containing the replacement entries. |
Returns#
The original target table. A non-table argument raises a type error.
Examples#
Update local settings without breaking references
local settings = { theme = "dark", oldOption = true }
local sameSettings = settings
local replacement = { theme = "light", fontSize = 16 }
local result = replacetable(settings, replacement)
assert(result == settings and sameSettings == settings)
print(settings.theme, settings.fontSize) -- light 16
print(settings.oldOption) -- nil