3

I have the following scenario in which the position of code shall not change. How to modify this code to fix the error without moving the function and table variable. I am a Lua newbie, just 4 days

function a()
  print("function a");
end

ftable = {
  ["a"] = a,
  ["b"] = b
};

function b()
  print("function b");
end

ftable["a"](); -- prints 'function a' 
ftable["b"](); -- attempt to call field 'b' (a nil value)

Update : Using the following mechanism its possible to do this.

function a()
  print("function a");
end

ftable = {
  ["a"] = a,
  ["b"] = "b"
};

function b()
  print("function b");
end

ftable["a"](); -- prints 'function a' 
_G[ftable["b"]](); 
Anish
  • 1,490
  • 1
  • 13
  • 13
  • 2
    that's not possible. you should rather ask yourself why you want to do this in the first place... you could assign your function to ftable.b though – Piglet Sep 19 '17 at 14:13
  • 2
    @Anish please remove your answer from the question and post it as an answer. Once you are satisfied with the answers, you can accept one—even if it is yours. (Yours could be improved by adding an explanation.) – Tom Blodget Sep 19 '17 at 16:17
  • 1
    The last line should be `ftable[_G["b"]]()`. – lhf Sep 20 '17 at 10:20

2 Answers2

4

Lua's table declaration and function definition (and function calling syntax) is very flexible. You can use the field key as an identifier instead of the index syntax, provided that the key is a string and is also a valid identifier ({ a = a } and ftable.a()). This seems to be the case with your a and b keys. You can also make the field assignment with the function definition statement (function ftable.b…).

function a()
  print("function a")
end

ftable = {
  a = a
}

function ftable.b()
  print("function b")
end

ftable.a() -- prints 'function a' 
ftable.b() -- prints 'function b' 

Of course, you could also move "function a" down and code it like "function b". that would leave ftable = { } at the top.

One difference from your code. The global variable b is no longer set to the function. In most cases, this would be considered an advantage.

I would also like to re-iterate what @lhf said,

There are no declarations in Lua, only definitions.

A function definition (in the general case) is an expression. When evaluated, it produces a function value. Various types of statements can assign the value to a variable or field, as does function ftable.b()….

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
3

You can't do that. There are no declarations in Lua, only definitions. You need to define variables before using them.

What you can do is to register the names of the functions in ftable and then fix the values before using them:

ftable = {
  ["a"] = true,
  ["b"] = true,
}
...
for k,v in pairs(ftable) do ftable[k]=_G[k] end

This code assumes that your functions are defined globally.

lhf
  • 70,581
  • 9
  • 108
  • 149