Backport __pairs and __ipairs metamethods

As a Roblox developer, it is currently too difficult to overload pairs and ipairs. Backporting these metamethods would allow for user-made Abstract Data Types (ADT) to have custom traversal behaviour. Some examples of use cases are:

  • Traversing user-made linked lists
  • Custom traversal behaviour for user-made binary trees
  • Custom traversal behaviour for user-made matrices
  • Custom traversal behaviour for any other user-made ADT

In addition to ADTs, backporting the metamethods will allow you to traverse proxy tables that are designed to conceal behaviour from other tables. As it currently stands, the existing metamethods allow you to create a complete proxy for a table without being aware that the proxy exists, except when you try to iterate through that proxy table. The __pairs and __ipairs metamethods would solve this problem.

If Roblox is able to address this issue, it would improve my development experience because it would allow for simple and intuitive control over the traversal of ADTs and proxy tables. Currently, developers need to overload the pairs and ipairs functions for each different type of use case if they want custom traversal, which is not a feasible solution for large projects.

4 Likes

Here’s the official take from zeux who works on Luau.


Not sure if things have changed since the making of the PDF but it seems that they would prefer introducing a new metamethod such as __iter, since customizing the standard library is icky.
1 Like