teal-language / tl

The compiler for Teal, a typed dialect of Lua
MIT License
2.1k stars 108 forks source link

should not used # on map with number key type #559

Closed virusdefender closed 1 year ago

virusdefender commented 1 year ago
x = {[1]="a", [2]="b", [5]="c"}
print(#x)

will get 2

x ={a="a", b="b", c="c"}
print(#x)

will always get 0

This is probably a bug in your code, it not equal to map_key_number(var)


If the key inserted to the map is in order (like 1, 2, 3), the result it correct, so # on map of number key will throw a warning instead of error.

github-actions[bot] commented 1 year ago

Teal Playground URL: https://559--teal-playground-preview.netlify.app

virusdefender commented 1 year ago

This is not a bug, this is how Lua works. The # operator counts the number of contiguous keys in the array part of the table.

this is not a Lua bug, if you use # on the map, it may cause logic bug in your code, because you may think it's equal to map_key_number(var)

hishamhm commented 1 year ago

Merged manually, thank you!!