Overloading Lua Functions

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.



-- functions (defined in C++ using dostring)
		"function TypeA:f ( szName )
"
			"Msg( 'inside first before' )
"
			"a:f( NULL, szName, NULL, NULL, NULL )
"
			"Msg( 'inside first after' )
"
		"end
"
		"function TypeA:f ( arg1, szName, arg3, arg4, arg5 )
"
			"Msg( 'inside second' )
"
		"end
" 
		"function TypeA:f ( arg1, szName, arg3, arg4, arg5, arg6 )
"
			"Msg( 'inside last' )
"
		"end
"


and it has the simple call:



"a:f ( 'test_01' )
"


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 :smiley:

I just had to change the ‘&&’ to ‘and’ for everything to work fine.
e.g. elseif(arg1 and arg2) then

Thanks again :slight_smile:

‘and’ and ‘&&’ are the same, there’s no need to change that.

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]