Is there a way to use inside a Lua table?

  Kiến thức lập trình

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}
}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT