Question about variable declaration

Hi, and thanks for reading in advance.

Can entries in a dictionary use or concatenate previous entries in the same dictionary? For example:

local list = {
     A = 5,
     B = 7,
     Add = function()
          print(A + B)
     end,
     Text = "The sum of A and B is.."..(A + B),
}

Would the last two entries be legal operations? If not, is there some way to achieve this without making a declaration outside the table?

No, but you could do list.A + list.B

and you would somehow have to call the Add function before setting the Text.

Text = "The sum of A and B is.."..(A + B) <---- This won’t work

Your add function can work, but it needs some changes

local list = {
     A = 5,
     B = 7,
     Add = function(self)
          print(self.A + self.B)
     end
}

list:Add()

When you call a function in a table using :, self is passed to the function, and that refers to the table housing the function.

1 Like