kts.table_merge – merge two Lua tables
require("base")
result = kts.table_merge(table1, table2)
This creates a new table which contains all keys from both table1 and table2, together with the corresponding values.
If the same key exists in both table1 and table2, then the value from table2 is preferred.
A new table, consisting of the merged keys and their corresponding values, is returned.
If the inputs are not valid tables (or table-like objects) then errors will result.
Unlike all other kts functions, kts.table_merge is not actually a built-in part of the kts table; instead, it is added to the kts table when the base module is loaded.
This could be seen as a bug – it would have been better if the function was just part of the base module in the normal way (i.e. base.table_merge not kts.table_merge) – but in practice, this isn't too serious, because most modules end up importing base anyway.
Consider the following code:
table1 = {a=1, b=2, c=3}
table2 = {c=99, d=98, e=97}
table3 = kts.table_merge(table1, table2)
After this code runs, table3 will be equal to {a=1, b=2, c=99, d=98, e=97}.
The kts.table_merge function is often used when we want to define a "base" version of some table, and then make several "variations" of it by adding new keys. An example of this can be seen in tiles.lua, where a door_base table is defined, and then wood_door and iron_door tables are derived from it using table_merge calls.