Need help,
I have a built-in module for complex numbers defined as follows:
int complex_number_new(lua_State* L)
{
// make a complex number and put it on the stack
return 1;
}
static const struct luaL_Reg s_complex_f[] =
{
// constructor
{ "new", complex_number_new },
// many more functions …
{ 0, 0 }
};
static const struct luaL_Reg s_complex_m[] =
{
{ "__call", complex_number_new },
// many more operators …
{ 0, 0 }
};
int luaopen_complex_number(lua_State* L)
{
//
luaL_newmetatable(L, "ak_complex");
//
luaL_register(L, 0, s_complex_m);
luaL_register(L, "complex", s_complex_f);
return 1;
}
Question: Why do I get the error “attempt to call global ‘complex’ (a table value)” when I attempt x = complex (1, 2), which should return the same thing as complex.new(1, 2)?
I see this question in the forum, but I (for the life of me) don’t understand the responses. What is special about __call, and why isn’t it better documented? I am so thick that it is going to take a concrete example in c or c++ to breach the wall.
What does work is
setmetatable(complex, { __call = function( _, ... ) return complex.new( ... ) end })
but I want to do this internally, not in Lua.
--BillF