Skip to content

Commit c08c2fc

Browse files
authored
Update table docs (#1092)
## Changes * Removes reccomendation of `pairs()` and replaces it with generic iteration * Renames the deepCopy function to deepClone * Makes the deepClone function use table.clone * Fix deepFreeze erroring if a value is already frozen * Explains dot indexing for dictionaries `.key` * Removes warning for using ipairs to iterate over a dictionary, as ipairs is unnessary to use unless wanting to stop at the first nil in a array with holes (a behavior that is very rarely desired) The last one is a bit iffy so feel free to reverse it. Also unsure if the wording for `Write to dictionaries` is the best, but I think the added warning clears it up. ## Checks By submitting your pull request for review, you agree to the following: - [x] This contribution was created in whole or in part by me, and I have the right to submit it under the terms of this repository's open source licenses. - [x] I understand and agree that this contribution and a record of it are public, maintained indefinitely, and may be redistributed under the terms of this repository's open source licenses. - [x] To the best of my knowledge, all proposed changes are accurate. ---------
1 parent 84acd44 commit c08c2fc

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

content/en-us/luau/tables.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ local testDictionary = {
149149

150150
### Read from dictionaries
151151

152-
To read from a dictionary, add a pair of brackets after its reference and specify the key name. Directly reference a string key using quotes (`["key"]`) or use a variable value (`[key]`).
152+
To read from a dictionary, add a pair of brackets after its reference and specify the key name. Directly reference a string key using either (`["key"]`) or (`.key`), or instead use a variable value (`[key]`).
153153

154154
```lua
155155
local part = Instance.new("Part")
@@ -160,13 +160,15 @@ local testDictionary = {
160160
}
161161
-- Include quotes for string keys
162162
print(testDictionary["partType"]) -- Block
163+
-- Or use . to index string keys without spaces
164+
print(testDictionary.partType) -- Block
163165
-- Omit quotes for non-string keys
164166
print(testDictionary[part]) -- true
165167
```
166168

167169
### Write to dictionaries
168170

169-
To define or rewrite the value of a new or existing dictionary key, declare the key name in brackets (`[key]`) followed by `=` and the value:
171+
To define or rewrite the value of a new or existing dictionary key, declare the key name in brackets (`[key]`) or, if the key is a string, use (`.key`) followed by `=` and the value:
170172

171173
```lua
172174
local testDictionary = {
@@ -176,19 +178,19 @@ local testDictionary = {
176178

177179
-- Change value of existing keys
178180
testDictionary["fruitName"] = "Cherry"
179-
testDictionary["sour"] = false
181+
testDictionary.sour = false
180182

181183
-- Insert new key-value pair
182-
testDictionary["fruitCount"] = 10
184+
testDictionary.fruitCount = 10
183185

184-
print(testDictionary["fruitName"]) -- Cherry
185-
print(testDictionary["sour"]) -- false
186-
print(testDictionary["fruitCount"]) -- 10
186+
print(testDictionary.fruitName) -- Cherry
187+
print(testDictionary.sour) -- false
188+
print(testDictionary.fruitCount) -- 10
187189
```
188190

189191
### Iterate over dictionaries
190192

191-
To iterate over a dictionary, use the global `pairs()` function in a `for` loop:
193+
To iterate over a dictionary, use a `for` loop:
192194

193195
```lua
194196
local testDictionary = {
@@ -197,7 +199,7 @@ local testDictionary = {
197199
sour = true
198200
}
199201

200-
for key, value in pairs(testDictionary) do
202+
for key, value in testDictionary do
201203
print(key, value)
202204
end
203205

@@ -208,10 +210,6 @@ fruitColor Yellow
208210
]]
209211
```
210212

211-
<Alert severity="warning">
212-
Unlike using `ipairs()` on an array, using `pairs()` on a dictionary doesn't necessarily return items in the same order that they're in the dictionary.
213-
</Alert>
214-
215213
### Remove key-value pairs
216214

217215
To remove or erase a key-value pair from a dictionary, set its value for a key to `nil`.
@@ -223,9 +221,9 @@ local testDictionary = {
223221
sour = true
224222
}
225223

226-
testDictionary["sour"] = nil
224+
testDictionary.sour = nil
227225

228-
for key, value in pairs(testDictionary) do
226+
for key, value in testDictionary do
229227
print(key, value)
230228
end
231229
--[[ Resulting output:
@@ -280,20 +278,21 @@ local clone = table.clone(original)
280278
To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following:
281279

282280
```lua
283-
-- The function used for deep copying a table
284-
local function deepCopy(original)
281+
-- The function used for deep cloning a table
282+
local function deepClone(original)
285283
-- Define the new table for the copy
286-
local copy = {}
284+
local clone = table.clone(original)
287285

288-
-- Loop through the original table to clone
286+
-- Loop through the original table to check for table values
287+
-- If a table is found as a value, deep clone it to the key (index)
289288
for key, value in original do
290-
-- If the type of the value is a table, deep copy it to the key (index)
291-
-- Else (or) the type isn't a table, assign the default value to the index instead
292-
copy[key] = type(value) == "table" and deepCopy(value) or value
289+
if type(value) == "table" then
290+
clone[key] = deepClone(value)
291+
end
293292
end
294293

295294
-- Return the finalized copy of the deep cloned table
296-
return copy
295+
return clone
297296
end
298297
```
299298

@@ -313,7 +312,7 @@ local original = {
313312
}
314313
}
315314

316-
local clone = deepCopy(original)
315+
local clone = deepClone(original)
317316
```
318317

319318
## Freeze tables
@@ -345,8 +344,9 @@ local function deepFreeze(target)
345344
table.freeze(target)
346345

347346
-- Check each key of the table and freeze it if it's a table
348-
for _, v in target do
349-
if type(v) == "table" then
347+
for _, value in target do
348+
-- Make sure the value isn't frozen; if it already is, an error will occur
349+
if type(value) == "table" and table.isfrozen(value) == false then
350350
deepFreeze(v)
351351
end
352352
end

0 commit comments

Comments
 (0)