How can I extend my __index metamethod so that it works regardless of the level of nesting? My current approach works only for data.raw.a1.a2, but not for data.raw.b1.b2.b3 and following.
data = {
raw = {}
}
setmetatable(data.raw, { __index = function(t,k)
t[k] = {}
return t[k]
end })
data.raw.a1.a2 = { foo = "bar" }
data.raw.b1.b2.b3 = { foo = "bar" } -- throws an error
data.raw.c1.c2.c3.c4 = { foo = "bar" } -- throws an error
I tried adding a second setmetatable but this only works for one more level of nesting.
data = {
raw = {}
}
setmetatable(data.raw, { __index = function(t,k)
t[k] = {}
-- below is new
setmetatable(t[k], { __index = function(t,k)
t[k] = {}
return t[k]
end })
-- above is new
return t[k]
end })
data.raw.a1.a2 = { foo = "bar" }
data.raw.b1.b2.b3 = { foo = "bar" } -- now this works
data.raw.c1.c2.c3.c4 = { foo = "bar" } -- this still throws an error
I don't know how I can make this recursive, so that it works for all levels of nesting. Send help.
Use a constructor function to create tables with a metatable that has an
__indexmetamethod that recursively calls the constructor function to create new table depths when an indexed key is not present.Here is a condensed example, but this can be written to reuse the metatable.
Result: