Yeah. One of the best things I've encountered in lua is the anything as references property.
There's nothing wrong with the way you are using your key
s in the table.
Tables in Lua are neither values nor variables; they are objects.You may think of a table as a dynamically allocated object; your program only manipulates references (or pointers) to them. There are no hidden copies or creation of new tables behind the scenes.
In your example, you haven't passed any argument to the function, so basically, it'll be useless in your case to have functions as 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
does have its uses.
Can Lua reuse function references once they are out of scope?
As long as your parent table is in scope, yes. Since tables are created and manipulated but not copied, so there isn't a chance that your function reference can be deprecated from the table index memory. I'll edit this answer later after trying it out actually too.
Can this create any issues or is it considered a bad practice due to some reason?
It's just considered a bad practise because users familiar with other languages, like C
, python
etc. tend to have array in mind when reading tables. In lua you have no such worries, and the program will work perfect.
I don't know if I can rely on the uniqueness of the function references.
Why?