mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
Update syntax.md
This commit is contained in:
parent
73ea6e9c89
commit
e562fff55b
1 changed files with 11 additions and 2 deletions
|
@ -207,6 +207,15 @@ for k, v in {1, 4, 9} do
|
|||
end
|
||||
```
|
||||
|
||||
This works for tables but can also be extended for tables or userdata by implementing `__iter` metamethod that is called before the iteration begins, and should return an iterator function like `next` (or a custom one).
|
||||
This works for tables but can also be extended for tables or userdata by implementing `__iter` metamethod that is called before the iteration begins, and should return an iterator function like `next` (or a custom one):
|
||||
|
||||
For tables, the iteration order is specified to be consecutive for elements `1..#t` and unordered after that, visiting every element; similarly to iteration using `pairs`, modifying the table entries for keys other than the current one results in unspecified behavior.
|
||||
```lua
|
||||
local obj = { items = {1, 4, 9} }
|
||||
setmetatable(obj, { __iter = function(o) return next, o.items end })
|
||||
|
||||
for k, v in obj do
|
||||
assert(k * k == v)
|
||||
end
|
||||
```
|
||||
|
||||
The default iteration order for tables is specified to be consecutive for elements `1..#t` and unordered after that, visiting every element; similarly to iteration using `pairs`, modifying the table entries for keys other than the current one results in unspecified behavior.
|
||||
|
|
Loading…
Add table
Reference in a new issue