Can lua function links be used as table keys?

Lua is new here. Can I store function references as keys in a Lua table? Something like this:

local fn = function() print('hello') end local my_table = {} my_table[fn] = 123 

This works fine, but I don't know if I can rely on the uniqueness of function references. Can Lua reuse function references after they are out of scope? Could this create any problems or for some reason is considered bad practice?

+4
source share
4 answers

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.

From Lua PiL

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

+5
source

The uniqueness of function references depends on the version of Lua.

Lua 5.2 Guide:
a function definition may not create a new value; it may reuse some previous value if there is no observable difference to the new function

Example:

 -- 10 different function references on Lua 5.1 -- The same function reference for Lua 5.2 local j for i = 1, 10 do print(function() print(j) end) end -- 10 different function references for any Lua version for i = 1, 10 do print(function() print(i) end) end 

So this is the rule: create different locks to get different links.

+3
source

You can do this, the function will remain in the same memory location until it is destroyed (by the garbage collector).

You can try in interactive lua :

 > fn = function() print('hello') end > print(fn) function: 0x9d058d0 > fn2 = function() print('hello') end > print(fn2) function: 0x9d05ee8 > fn2=fn > print(fn2) function: 0x9d058d0 > print(function() print('hello') end) function: 0x9d068a8 > print(function() print('hello') end) function: 0x9d06bf8 

Remember that if fn is local , it will be garbage collection when it goes out of scope. In your case, this should not be a problem, since my_table is local in the same block. See the Lua documentation on local variables .

+1
source

This works fine, but I don't know if I can rely on the uniqueness of function references.

Each function() end statement creates a new closure. The actual function code will be reused if it comes from the same constructor:

 for i=1,100 do t[function() print(i) end] = i -- this function body will be reused end 

But each close will be unique, and this is important for your reference.

Can Lua reuse function references after they are out of scope?

Lua is not going to collect fn while it is used as a key in my_table . If my_table goes beyond the scope too, so both the table and the function are collected, then reusing the Lua link will not affect you. Anyway, you're good.

+1
source

All Articles