Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can lua function references be used as table keys?

A Lua newbie here. Can I store function references as keys in a Lua Table? Something similar to this:

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

This does seem to work fine but I don't know if I can rely on the uniqueness of the function references. Can Lua reuse function references once they are out of scope? Can this create any issues or is it considered a bad practice due to some reason?

like image 357
Vivek Avatar asked Jan 28 '13 14:01

Vivek


People also ask

Is Lua pass by reference?

Lua's function , table , userdata and thread (coroutine) types are passed by reference. The other types are passed by value.

Is everything a table in Lua?

Good article, but one nitpick: not everything in Lua is a table. Tables are a versatile data structure that can be used as arrays, dictionaries, objects, etc.

How does a table work in Lua?

Tables are the only data structure available in Lua that helps us create different types like arrays and dictionaries. Lua uses associative arrays and which can be indexed with not only numbers but also with strings except nil. Tables have no fixed size and can grow based on our need.

Does Lua reference?

As the name implies, we use references mainly when we need to store a reference to a Lua value inside a C structure. As we have seen, we should never store pointers to Lua strings outside the C function that retrieved them. Moreover, Lua does not even offer pointers to other objects, such as tables or functions.


1 Answers

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 keys in the table.

From Lua PiL

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?

like image 70
hjpotter92 Avatar answered Nov 15 '22 05:11

hjpotter92