lua-users home
lua-l archive

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


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.

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

Set: now there is something one might provide special syntax for,
since one must choose between the devil and the deep blue sea:
either the inefficient conversion that you mention or the tedious and
barely legible {[1]=1,[2]=1,[3]=1}. It would be profligate to squander
on a non-problem like an array, syntax space that could have been
used for a real need.

> .. and then later the first array is GCed. Not good if you repeat
> 10,000 times. Unless of course you intend to coerce the table
> in-place into an array, which is not very constructor-y.

In Lua 6.0 we might have a length field [1] in Table, hidden from
pairs and ipairs, but accessible by #, getlen and setlen, and
exploitable by a constructor. This was set to 3 when the table
was constructed from a literal.

[1] Only the hope for that keeps me in threads like this one.