lua-users home
lua-l archive

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


On 07/13/2016 01:41 PM, Egor Skriptunoff wrote:
On Wed, Jul 13, 2016 at 9:32 AM, Thomas Jericke <tjericke@indel.ch> wrote:
On 07/12/2016 10:58 PM, Rena wrote:
If there were going to be new syntax, I feel it'd go well with the discussion of arrays:

t = {x, y, z} --same as current meaning
a = [x, y, z] --means: a = array(x, y, z)
Where array() is something like:

function array(...)
  local t = {...}
  return setmetatable(t, {
    __n = select('#', ...),
    __index = <snip>,
    __newindex = <snip>,
    ...etc...
  })
end

I think Lua would benefit from a "standard" array type, even if it is just a table. (Being standard does also mean the possibility to use a more optimal internal representation.) When everyone rolls their own, interoperability is difficult.

As much as I would like that, the syntax clashes with existing statements.

t[1] -- Call t with single element array or indexing with 1?
t[[1,2,3]] -- Call t with string "x" or indexing it with t array?


The solution is simple: use space to separate lexems.
We already have similar "features" in Lua:
x = t[ [=[a]=]]   -- x = t.a
y = t[[=[a]=]]    -- y = t("=[a]=")


That solves only the second case (and isn't very nice in doing so). But what about the way more common and important first case?

The only good solution I see is to not allow function calls by array constructors. So you would always have to write:
t([])
t([1])
t([1,2])
...

Disallowing only the single element case would be very error prone IMO.
--
Thomas