lua-users home
lua-l archive

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


On Wed, Aug 18, 2004 at 06:13:48PM -0700, Marc Nijdam wrote:
> a = {
>    b = {
>       c = 22;
>    }
> }
> 
> d = clone{a,
>   b.c = 33;
> }
> 

If your table keys are always identifiers, you could do

d = clone{a,
	["b.c"] = 33
}

and have the clone code check for dots in the keys and act
accordingly.

Another possiblity might be to clone a, and then recursively merge in
keys from a second table.

d = clone(a, {
	b = { c = 33 }
})

That has problems if the 'leaf' values in your structure can be
tables, and you want at some depth to just overwrite rather than
continuing to merge. You could fix that with

d = clone(a, {
	b = { c = 33, d = protect(leaf_table) }
})

where protect is a function which returns a special value (e.g. a
table with a certain metatable), which indicates to your merge
function that d is to be overwritten. 

There are a few other possible approaches too. It all depends on the
details...

-- Jamie Webb