lua-users home
lua-l archive

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


On 27.07.2016 07:29, Tim Hill wrote:

On Jul 26, 2016, at 12:30 AM, Thomas Jericke <tjericke@indel.ch> wrote:

What I thought was that I need a good syntax for simple sequence table constructors as well. e.g t = {x, y, z}

First try
{ x, y, z, # }
is syntactic sugar for

{ [1] = x, [2] = y, [3] = z, [#] = # }

A simply # in the constructor sets t[#] at the current length (t[#] = #t). I am not sure about it though, as # acts here as an operator and not
as a value.

A bigger issue is that Lua makes no guarantees about the order of initializers, so you don’t know what the value of t[#] will be.

—Tim

Definition: If # is used as table key in a table constructor the according statement is always evaluated last, if # is used more than once in a single table constructor the order of execution between those statements is undefined.

There you go. Not that I like it, but not really an issue. Also when I use the debugger, so far I have only seen Lua to evaluate table constructors in order, so a solution would be to just grantee the most obvious behavior.

--

Thomas