mirror of
https://github.com/luau-lang/luau.git
synced 2024-12-13 13:30:40 +00:00
Update generalized-iteration.md (#640)
Use a more precise pseudocode snippet for __iter handling which matches runtime semantics
This commit is contained in:
parent
4ded555cc5
commit
0e118b54bb
1 changed files with 4 additions and 2 deletions
|
@ -51,8 +51,10 @@ end
|
||||||
To support self-iterating objects, we modify the iteration protocol as follows: instead of simply expanding the result of expression `iter` into three variables (`gen`, `state` and `index`), we check if the first result has an `__iter` metamethod (which can be the case if it's a table, userdata or another composite object (e.g. a record in the future). If it does, the metamethod is called with `gen` as the first argument, and the returned three values replace `gen`/`state`/`index`. This happens *before* the loop:
|
To support self-iterating objects, we modify the iteration protocol as follows: instead of simply expanding the result of expression `iter` into three variables (`gen`, `state` and `index`), we check if the first result has an `__iter` metamethod (which can be the case if it's a table, userdata or another composite object (e.g. a record in the future). If it does, the metamethod is called with `gen` as the first argument, and the returned three values replace `gen`/`state`/`index`. This happens *before* the loop:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
if getmetatable(gen) and getmetatable(gen).__iter then
|
local genmt = rawgetmetatable(gen) -- pseudo code for getmetatable that bypasses __metatable
|
||||||
gen, state, index = getmetatable(gen).__iter(gen)
|
local iterf = genmt and rawget(genmt, "__iter")
|
||||||
|
if iterf then
|
||||||
|
gen, state, index = iterf(gen)
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue