Search code examples
functionluacall

How to call unnamed function in a lua table? [from outside the table]


p={update=function(} print("hello") end}

I can call the function within p using p.update() outside the table.

How do I call unnamed functions though? i.e.

p={function(} print("hello") end}

If I know the index of the function, 1 in this case, can I use that in my function call? like p1? I've tried some variations of this to no success.


Solution

  • Yes. p.update is just syntactic sugar for one-word string keys.

    The more general way to index a table is to put the key value in brackets:

    p[1]()