Since Lua 5.4, the <const>
syntax lets us set const variables. I noticed that this doesn’t transitively affect fields inside a table.
local x <const> = {
a = {1,2,3},
b = {5,6,7}
}
x = 5 -- error
x.a = 9 -- no error
The table x
is const and cannot be reassigned, but fields inside the table can.
Is there any way to make the fields inside a table also const via syntax alone? I know that it’s possible via the index and newindex metamethods, but I’m curious if it’s possible with simple syntax like <const>
.
I tried the following, but it produces a syntax error:
local x <const> = {
a <const> = {1,2,3},
b <const> = {5,6,7}
}