2 ms·
Like many Lua developers, I have an extended table library containing the following functions: `clone1`, which is a fast, jittable implementation of a shallow c
by samatman 4y ago
Like many Lua developers, I have an extended table library containing the following functions: `clone1`, which is a fast, jittable implementation of a shallow clone from the openResty fork, `clone(tab, depth)` which defaults to one and performs a shallow clone to `depth` which will unroll cycles, `cloneinstance` which recursively clones everything with cycle detection, but gives copies the same metatable as their original, and `deepclone`, which will also clone metatables. `clonearray` could exist but I haven't found a frequent need for it.
Hence the reason cloning is a library function rather than core: when you have only one data structure, there are a lot of ways to copy it, and that's before getting into the various ways that metamethods can override the default behaviors of a table. Do you want to clone every pair in the table, or do you want to clone only the ones returned by `pairs`, which has a metamethod which can override the default behavior?
There are a dozen decisions like this to make.