evaera / matter

A modern ECS library for Roblox.
https://eryn.io/matter/
MIT License
143 stars 34 forks source link

Use __iter [in addition to?] __call #14

Closed zeux closed 2 years ago

zeux commented 2 years ago

Our implementation of __iter (RFC) currently respects __call and treats it as a sign that we should not iterate through key/value pairs of the table to preserve backwards compatibility. __iter is currently in Studio beta (https://developer.roblox.com/en-us/resources/release-note/Release-Notes-for-526).

Long term it would be nice if we could clean this up and remove the special casing of __call during iteration. This requires that all code that currently uses __call in iterators to migrate to __iter. Matter is one of very few (two for now based on our analytics) libraries that does that.

Would it be possible to specify __iter on iteratable objects? You should be able to do this in a way that doesn't break when __iter is not implemented by simply declaring it as a forwarding method, something like

function Iterator.__iter(self)
  return self
end

(which will still call __call on every iteration after that, __iter is only called once before the iteration begins)

Alternatively you can return an actual iteration function from __iter which will be faster since __call calls are slower than regular function calls, but I haven't looked closely at Matter source to see if this would be easy to do. In either case the change would be backwards compatible if you don't touch __call so it can be implemented before __iter goes out of beta I believe.

evaera commented 2 years ago

Matter now uses both __iter and __call for iteration over QueryResult. We can remove __call once __iter is stable