lua-users home
lua-l archive

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


> I have an application in which new tables tend to get created
> unnecessarily.
> 
> E.g.
> 
>     myfunc(x)
> 
> returns a table with the same keys as x. When called with
> an anonymous table, e.g.
> 
>    myfunc{1,2,3,4,5}
> 
> it is OK to modify the table and return it, but when called with
> a table that already has a name, one must make a new copy
> in case that name is referenced later.
> 
> Is there a way of diagnosing this difference?

A simple solution is to add a parameter that specifies if you want a copy or
not:

local _myfunc = myfunc
myfunc = function (x, copy)
  return _myfunc(copy and copy(x) or x)
end

Cheers,
Luis

-- 
Computers are useless. They can only give you answers.
                -- Pablo Picasso

-- 
Luis Carvalho (Kozure)
lua -e 'print((("lexcarvalho@NO.gmail.SPAM.com"):gsub("(%u+%.)","")))'