Lua: Binding a function with optional parameters

BlitzMax Forums/BlitzMax Programming/Lua: Binding a function with optional parameters

Gabriel(Posted 2008) [#1]
I'm trying to bind a function to Lua which has optional parameters in BlitzMax. Ideally, I would prefer to set those default values in BlitzMax, and not in the lua-side bindings. Now I know I can use lua_gettop to count the parameters on the stack. That part is ok. What I'm having a problem with is binding it correctly on the Lua side.

Now Lua features dynamic (and somewhat weak) typing, so I cannot overload the functions the way I could in C, so I can only have one function prototype regardless of however many parameters I send. So that means - as I see it - that I'm going to be sending the maximum number of parameters regardless of whether or not I specify them in my Lua code, because the bindings will have to pass all parameters, unless I checked them all in Lua, which seems messy.

So I'm not really sure if there's an elegant way to do this. Either I check all the parameters to see if they're Nil when they get received in BlitzMax, or I check if they're Nil in the Lua bindings and if they are, I pass less parameters. Neither seems particularly elegant. Is there a better way to do this?


Tommo(Posted 2008) [#2]
I remember rozek's blitzlua module gives some helper functions on the parameters checking(located in lua.mod/support.mod). It comes with customed lua error message, which is helpful.


Gabriel(Posted 2008) [#3]
I'm not sure I understand what you're suggesting. I don't think I can use BlitzLua, as I'm using my own modules, and writing my own bindings, which I think BlitzLua isn't for. I do use Andreas Rozek's other Lua module though, and it may be that the helper functions are in here too. Are you suggesting that I go with sending all the parameters and then using the helper functions to see which values are nil so that I can ignore them?