You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An implementation I have done in the past converts the following:
forx, y, zininputdoprint(x, y, z)
end
into:
local_iterator=inputlocal_invariant, _controliftype(input) =="table" thenlocal_mt=getmetatable(input)
iftype(_mt) =="table" andtype(_mt.__iter) =="function" then_iterator, _invariant, _control=_mt.__iter(input)
else_iterator, _invariant, _control=pairs(input) -- !endendforx, y, zin_iterator, _invariant, _controldoprint(x, y, z)
end
This may not be 100% correct but could be a good starting point.
As it can't be determined statically whether the subject of iteration is (1) a plain table, (2) an iterator function, or (3) a table with an __iter metamethod defined, it is necessary to insert a runtime check.
Notes:
Using pairs(input) does not necessarily conform to the order of Luau generic iteration (for example, consecutive array indices starting from 1 go first)
Doesn't handle iterating over userdata (with __iter defined), which is supported by Luau
There may also be issues with accessing __iter if __metatable is set
The text was updated successfully, but these errors were encountered:
See #202 for motivation.
An implementation I have done in the past converts the following:
into:
This may not be 100% correct but could be a good starting point.
As it can't be determined statically whether the subject of iteration is (1) a plain table, (2) an iterator function, or (3) a table with an
__iter
metamethod defined, it is necessary to insert a runtime check.Notes:
pairs(input)
does not necessarily conform to the order of Luau generic iteration (for example, consecutive array indices starting from 1 go first)__iter
defined), which is supported by Luau__iter
if__metatable
is setThe text was updated successfully, but these errors were encountered: