1. Post #1

    May 2009
    11 Posts
    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"
    and it has the simple call:
    Code:
    "a:f ( 'test_01' )\n"
    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.

  2. Post #2
    Gold Member

    July 2006
    1,819 Posts
    Lua doesn't have overloading as far as I know. Why not just to use something like this?

    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

    Edited:

    Lua doesn't have overloading as far as I know. Why not just to use something like this?

    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

  3. Post #3

    May 2009
    11 Posts
    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 :)

  4. Post #4
    Gold Member
    Silverlan's Avatar
    October 2005
    735 Posts
    I just had to change the '&&' to 'and' for everything to work fine.
    e.g. elseif(arg1 and arg2) then
    'and' and '&&' are the same, there's no need to change that.

    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 )
    [...]
    That's right, but you can easily do something similar in 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