lua-users home
lua-l archive

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


> On 2016-03-01, at 1:29 AM, Daurnimator <quae@daurnimator.com> wrote:
> 
> Yes. Many of these type of small functions have no wide-ly agreed upon
> semantics.
> e.g. for clone:
>  - will it respect __pairs?
>  - will it clone metatables?
>  - will it clone keys?
>  - what happens when cycles are encountered?

Microlight avoids these problems by just not implementing a clone. For a shallow clone, you'd probably use a special case of a general function:

update( {}, t )

"All the key value pairs from the input tables are copied into the first argument. Keys may be overwritten by subsequent tables."

You can start with any table you want, not just {}, so the choice of metatable is yours. If you have an object system, it may be useful to write  "update{ Point{}, p )". In any case, it's obvious what update does.

Because update is not a clone, it does not have to decide (or document!) whether it detects cycles or clones keys.

Perhaps it would be useful documentation as to whether update respects __pairs. (It does.)

Jay