lua-users home
lua-l archive

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


Thomas Hafner wrote:

>> There is no simple way of copying a table in the most general case
>> (because tables can self-reference). A simple one (which does not
>> treat well self-references) is:
>> [...]
>> But you could take a look at http://lua-users.org/wiki/CopyTable,
>> much better.

> Is there a standard library, where code like that belongs to? (I think
> it's Ok, that it isn't part of the *language* itself, so the language
> keeps being tiny.)

The main reason for the lack of an 'official' table copy is the problem
of what to do with self reference, either direct or indirect. It is a
problem whose solution depends very much upon the application. Manel's
solution, as noted, is dangerous when applied to an arbitrary table that
contains any cycles of self reference (don't try it on _G!).

> What a pitty, if not: then every developer performs
> the same tasks in a different way and no glue code can be easily
> exchanged/shared among developers. Just: take it all or nothing.

I think you may be judging too harshly too soon. This list, the module
system, and the wiki are all good ways to share code.

Over the years I've noticed that the Lua team tend to follow guidelines
when considering additions to the standard:

  (1) Necessity - the item cannot easily be provided by the programmer
(pairs(), for example).

  (2) Simplicity - the item should be easily understandable and not
result in a proliferation of special cases (tables as the single
container, for example).

  (3) Universality - the item must extend as much as possible in its
area of application (metatables, for example, especially now that they
extend to other types).

  (4) Utility - if it is not necessary then the item must provide
something quite useful (the module system, for example).

I think table.copy() as defined on the wiki meets criteria (2), (3), and
(4), but I can see how the Lua team might reject it due to (1). They
might point to the wiki and say, 'You can use *that* version of
table.copy() if it suits your purpose.'

But the guidelines are just guidelines, applied generally to the
entirety of Lua and not to a specific item. The authors have been doing
a pretty good job of keeping the balance between simplicity and utility.
I very rarely find myself in disagreement with a decision - and even
then I can usually work around the result. table.copy() is a border case
that could go either way.

Doug

-- 
Doug Rogers   03CF-7D28-E4B1-9A56 Least 4x4+Binary magic square
Innovative Concepts, Inc.  703-893-2007 (x220)  www.innocon.com