lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Beside obviously funny code like {1, [1]=2}, the issue is more serious with this code, not created just for fun by hackers:

{ [k(1)] = v(1), [k(2)] = v(2) }

Whose result is also completely undefined. There's no warranty that you'll find the two values v(1) and v(2) even if they are non-nil and different. The situation gets even worse if one of these values is nil, as you could get a table containing no value at all even if the second value was not nil.

Constructors with dynamic key expressions are flawed. Permitted and documented by the syntax and the standard parser. But you cannot know what will be actually in the table. And it is impossible to track at runtime even if you overload the __newindex metamethod.... Which you cannot even do with tabke constructors that always return a table without metatable.

The only safe way to initialize a table with keys is to create an empty table, then populate it in a loop item by item. The keys and values you'll use in the loop however can be stored in an unkeyed table constructor (creating sequences with implicit integer keys)

Explicit keys with dynamic values do NOT work at all as expected with  the constructor.

But populating a table in a loop is slow and costly, it overused the memory locator and stresses the GC. 

Le jeu. 23 juil. 2020 à 19:32, Martin <dyngeccetor8@disroot.org> a écrit :
On 2020-07-23 4:19 p.m., Coda Highland wrote:
> That said, arguing about this just indicates that people are talking on
> different levels. Nobody here is WRONG. But one side is arguing about
> what you SHOULD do, while the other is arguing about what you CAN do.

I think here lies distinction between hacker and programmer. Hacker wish
to exploit custom details to create tool for job. Programmer wish to
create product with easy maintainability and agnostic to hardware.

So they have different targets, different design considerations, but
their final product is software.

So exploiting special cases as "-0.0" or "{false, [1] = true}" is evil
thing for production code but neat hack to determine possible Lua
version in some game sandbox.

-- Martin