Yes. One of the best things I've come across in lua is the property as a reference .
There is nothing wrong with how you use key in a table.
Tables in Lua are neither values ββnor variables; they are objects. You can present the table as a dynamically allocated object; your program only manipulates links (or pointers) to them. There are no hidden copies or creating new tables behind the scenes.
In your example, you did not pass any argument to the function, so basically it will be useless for you to have functions as a reference in the program. On the other hand, something like this:
fn1 = function(x) print(x) end fn2 = function(x) print("bar") end t[fn1] = "foo" t[fn2] = "foo" for i, v in pairs(t) do i(v) end
has its uses.
Can Lua reuse function references after they go out of scope?
As long as your parent table is in scope, yes. Since tables are created and processed, but not copied, it is therefore not possible for a function reference to be out of date from the index memory of the table. I will edit this answer later, even having tried it.
Could this create any problems or for some reason is considered bad practice?
This is simply considered bad practice because users who are familiar with other languages ββsuch as C , python , etc., tend to consider the array when reading tables. In lua you have no such worries and the program will work perfectly.
I do not know if I can rely on the uniqueness of function references.
Why?
source share