lua-users home
lua-l archive

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


On Fri, Aug 1, 2014 at 8:15 AM, Daurnimator <quae@daurnimator.com> wrote:
> On 1 August 2014 10:48, Roberto Ierusalimschy <roberto@inf.puc-rio.br>
> wrote:
>>
>> >  [...] However, I am not delighted
>> > by an optional argument preceding a non-optional one. [...]
>>
>> We do not like it either. Do you suggest any alternative?
>>
>> -- Roberto
>>
>
> I see no problem saying making the function signature
>
>     table.icopy(from_tbl, from_idx, num_elements, [to_tbl, to_idx])
>
> If to_tbl and to_idx ar not given, they default to the empty table and 1.
> The function always returns to_tbl.

Ooh. I LIKE this. It handles a lot of things all at once, in a nice
little elegant solution. Built-in shallow copy for sequences with
obvious semantics? Check. Ability to duplicate elements within a
single table? Check. Ability to splice elements from one table into
another? Check.

I'd ponder over making from_idx and num_elements optional as well
(defaulting to 1 and #from_tbl) to allow "foo = table.icopy(bar)" a
convenient shorthand, but I can see merit in making them required even
if their values are obvious.

I wonder if there's a compelling use case for table.copy(from_tbl[,
keys[, to_tbl]]) that's a non-sequence equivalent; passing nil to keys
would mean "copy everything". The three-parameter form would serve a
purpose equivalent to Python's dict.update, while the one-parameter
form would be a shallow copy and the two-parameter form would be a
subsetting operation.

/s/ Adam