riv
riv

Reputation: 7324

Specifying function argument names in lua

I load the body of a function from a string (with C api), but by default, all arguments passed to the function are accessed with .... What is the best way to specify an argument list? I can only think of prepending a line like the following to the string before parsing it (assume the arguments should be self, x, y):

local self, x, y = ...

However, I'm not sure if its the best way to do this, or whether it has any unintended side effects.

Update: in one of the functions, I need an argument list of the form self, type, .... The following wouldn't work, right?

local self, type, ... = ...

Should I use this instead?

text = "return function(self, type, ...)" + text + " end";
luaL_loadbufferx(L, text, text.length(), filename, "t");
lua_call(L, 0, 1);

Upvotes: 2

Views: 376

Answers (1)

lhf
lhf

Reputation: 72312

Prepending that line is an excellent way to create named arguments. If you use a local declaration as you have, then there won't be any side effects (except the ones from the rest of the code).

Upvotes: 3

Related Questions