lua-users home
lua-l archive

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


-----Original Message----- 
> From: "Dirk Laurie" <dirk.laurie@gmail.com> 
> To: "Lua mailing list" <lua-l@lists.lua.org> 
> Date: 31-07-2016 11:52 
> Subject: Re: Quest: real world "Lua array with holes" usage 
> 
> 2016-07-30 22:50 GMT+02:00 Tim Hill <drtimhill@gmail.com>:
> >
> >> On Jul 29, 2016, at 12:34 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> >>
> >> 2016-07-29 8:20 GMT+02:00 Thomas Jericke <tjericke@indel.ch>:
> >>
> >>> Anyway I am currently think {(1, 2, 3)} is the more intuitive array
> >>> constructor syntax. This because (1, 2, 3) is already the syntax for an
> >>> argument list.
> >>> To me {(1, 2, 3)} reads like: I have this list here that you should back me
> >>> into a table please.
> >>
> >> The most intuitive array constructor syntax is
> >>
> >>    Array{1,2,3}
> >>
> >
> > The trouble is this is inefficient. Lua has to construct a regular table,
> > then pass it to the Array() function, which (presumably) then copies
> > the content to an array (how?)
> 
> No, it does not. That's a C++ notion. Copy constructors are foreign to
> Lua except in userdata. Any or all of tbl:copy(), tbl:clone(), tbl:deepcopy(),
> tbl:deepclone() may needed.
> 
> 'Array' sets a metatable and returns its argument. That's all. No copying.
> "return Array{tbl}" is a tail call. Not inefficient.

So I looked into the Lua 5.3 source a bit, and this is indeed not inefficient.
There is some overhead, as the function Array doesn't know the lenfth of the array,
it has to call the length operator. Also you need at least a function call more than
with a direct array declaration.

> 
> My point is that an array is only one of several plausible specializations
> of a table.

Now I see why you come to a different solution for the array syntax.
You see the array as a specialization of the table, while I just see it as
an additional feature of the current table. In my approach the Lua table would
still be the current "eierlegende Wollmilchsau" (egg-laying wool-milk-sow) that it
currently is, while you would start to separate the table into different animals for
each use case.

--
Thomas