I want to add table.isarray, but is my understanding of how it works correct? #457
-
I haven't done much digging into Luau (let alone stock Lua) but one of the methods that has repeatedly been needed is a method of determining if a table is an array or a dictionary. From what I can understand, a table is an array if Thanks ahead of time. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
While we often call some types of tables "arrays", I am not sure there's a reasonable formal distinction you can draw and if it is, what it would be, between arrays and non-arrays. For example, assuming you implemented
I don't think it's reasonable to specify queries in terms of internal behavior, but what would the meaningful external question be? For example:
Are these both arrays? Or are these both not arrays? You could claim that a table is an array if all its keys represent a sequence between 1 and #t with no gaps (no nil values). This is possible to specify, but it's not really free to check. Both of these can be checked in user space, and both of these can not be checked in constant time. Neither of these is obviously correct either I suppose. |
Beta Was this translation helpful? Give feedback.
-
Yes, this is what I meant when I said array. I am not sure why it didn't occur to me that real arrays can have holes, my mistake. I suppose its because I am so used to the concept of arbitrary keys or "holes" constituting what I see as dictionaries in Lua, even if that dictionary has integer keys. Just a mild misconception. I was tinkering with a hypothetical PR and playing with some code, and the method I suggested will work until a table has an element added to it that causes the "1 to #t" contract to be violated. Past that, due to the fact that the node map will always retain some size, a more complex test is required, so looking at it that way I don't think it's very worth it. I do have interest in other methods added to table that would have broader use cases (methods like In hindsight, a case where this |
Beta Was this translation helpful? Give feedback.
-
For what its worth, I remember having these thoughts/issues when starting with lua many years ago, but working with it daily now for many years I cannot remember an instance where I needed to check if something is an "array" or not. |
Beta Was this translation helpful? Give feedback.
-
Wanted to add my use case where IsArray may be useful: I am looking to possibly using Luau for a build system scripting language and the inability to tell what is and is not an array is odd to me. I am using Toml to load config state containing tables and arrays. This data will be processed by a scripting language (possibly Luau) to generate build graphs and shared state that has both tables and arrays to be used internally for the build system. Going from tables and arrays to just tables is straightforward, but I worry there will be a lot of edge cases and unexpected behavior where arrays are improperly handled and do not convert back to the correct type after invoking a Luau script. Has there been any consideration to extend Lua/Luau to include a true array type? Is there a strong reason that Lua decided to go with "Arrays are special Tables"? |
Beta Was this translation helpful? Give feedback.
While we often call some types of tables "arrays", I am not sure there's a reasonable formal distinction you can draw and if it is, what it would be, between arrays and non-arrays.
For example, assuming you implemented
isarray
as "hash part is dummynode", you'd get:I don't think it's reasonable to specify queries in terms of internal behavior, but what would the meaningful e…