2

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);
2
  • Your last solution is good. Just note that you need to call lua_call twice to run your code. Commented Apr 5, 2013 at 21:39
  • Yes, after the fragment I posted I grab the function from the stack and store it for later use. First solution has no lua_call. Commented Apr 5, 2013 at 22:53

1 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, great, though if for some reason my arguments needs to be (self, ...) - I can't do that, right?
You can eliminate the (self) from the arguments by using this: function Thing:functionName() (the :() is the same as saying self)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.