Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Awkward results from the length operator with multiple nil entries #51

Closed
MCJack123 opened this issue Apr 15, 2021 · 0 comments · Fixed by #62
Closed

Awkward results from the length operator with multiple nil entries #51

MCJack123 opened this issue Apr 15, 2021 · 0 comments · Fixed by #62

Comments

@MCJack123
Copy link
Contributor

While working on CraftOS-PC I noticed that Cobalt can give inconsistent results with the length operator when there are multiple nil areas in a table. This appears to be due to the binary search algorithm landing on specific entries and assuming they're on a boundary (which is true). Here's some examples of different results from very similar tables:

image

According to PUC Lua, these should be returning 9, 8, and 9 (as the contents are statically allocated*). A user might expect all three to resolve to 3, as the first boundary is at index 3.

I know the specification for the length operator is a bit ambiguous, and that these results are technically correct. However, they can be wildly variant, and since users often think it returns the boundary of the first nil they might be caught off-guard with statically allocated* tables of this nature. I'm not sure how this will affect speed, but it might be better for user experience to either a) return length using an array search first, then fall back on hash search (as PUC Lua does), or b) do a linear search (which is slower but more reliable to the user).


*When I use "statically allocated" I refer to creating a table with pre-filled slots, including nils in between. Not sure exactly how to refer to this, but whatever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant