• Overloading Lua Functions
    3 replies, posted
I understand that Lua doesn't allow type-based overloading like not being able to distinguish between: function f ( string, int ) and function f ( int, string ) but I'm wondering if it allows any overloading at all. I ask because I've encountered the following situation. I have a few overloaded functions I define and a call to one of them. [code] -- functions (defined in C++ using dostring) "function TypeA:f ( szName )\n" "Msg( 'inside first before' )\n" "a:f( NULL, szName, NULL, NULL, NULL )\n" "Msg( 'inside first after' )\n" "end\n" "function TypeA:f ( arg1, szName, arg3, arg4, arg5 )\n" "Msg( 'inside second' )\n" "end\n" "function TypeA:f ( arg1, szName, arg3, arg4, arg5, arg6 )\n" "Msg( 'inside last' )\n" "end\n" [/code] and it has the simple call: [code] "a:f ( 'test_01' )\n" [/code] When I run this call (well, I renamed things in this code to make it easier to understand), it always just goes straight to the last defined overload of the function i.e. it prints out "inside last" and nothing else. I would've expected it to go into the function with one parameter. Am I doing something wrong or is it that Lua simply doesn't have overloading? All help appreciated.
Lua doesn't have overloading as far as I know. Why not just to use something like this? [lua]function testfunc(arg1, arg2, arg3, arg4, arg5) if(arg1 && arg2 && arg3 && arg4 && arg5) then -- we have five arguments, do something with them elseif(arg1 && arg2 && arg3 && arg4) then -- we have four arguments elseif(arg1 && arg2 && arg3) then -- we have three arguments elseif(arg1 && arg2) then -- we have two arguments elseif(arg1) then -- we have one argument else -- we have no arguments at all end end[/lua] [editline]01:34PM[/editline] Lua doesn't have overloading as far as I know. Why not just to use something like this? [lua]function testfunc(arg1, arg2, arg3, arg4, arg5) if(arg1 && arg2 && arg3 && arg4 && arg5) then -- we have five arguments, do something with them elseif(arg1 && arg2 && arg3 && arg4) then -- we have four arguments elseif(arg1 && arg2 && arg3) then -- we have three arguments elseif(arg1 && arg2) then -- we have two arguments elseif(arg1) then -- we have one argument else -- we have no arguments at all end end[/lua]
Oh wow. That's so brilliantly simple. I'll do that. Thank you very much :D I just had to change the '&&' to 'and' for everything to work fine. e.g. elseif(arg1 and arg2) then Thanks again :)
[QUOTE=Phloxicon;16368628] I just had to change the '&&' to 'and' for everything to work fine. e.g. elseif(arg1 and arg2) then [/QUOTE] 'and' and '&&' are the same, there's no need to change that. [QUOTE=Phloxicon;16368436]I understand that Lua doesn't allow type-based overloading like not being able to distinguish between: function f ( string, int ) and function f ( int, string ) [...] [/QUOTE] That's right, but you can easily do something similar in lua: [LUA] local function f(a,b) if type(a) == "string" && type(b) == "number" then // Parameter 'a' is a string, parameter 'b' is a number elseif type(a) == "number" && type(b) == "string" then // Parameter 'a' is a number, parameter 'b' is a string end end [/LUA]
Sorry, you need to Log In to post a reply to this thread.