Question: why overload always calls the first proc in the list?

I am finding many problems using overload in OOP.

But for starter, here is a basic one that does not use Object. Will make separate question for other issues once I figure this basic one out.

I have overload with two procs.

First proc takes 2 arguments of type string, and the second proc one argument also of type string.

When calling the overloaded proc with one argument or two arguments, it always calls the first one which takes 2 arguments.

It never calls the second, even though the call is using one argument only. It always matchs the first proc.

It seems Maple just checks if the first argument match and stops.

I also made sure to use $ for end of arguments flag.

Now when changing the order and putting as the first proc the one that take one argument and the second proc which takes two arguments, now it works. Maple calls the correct proc based on the number of arguments.

How could this be possible? Is order of procs in overload important?? How does one then figure the correct order. It should be based on match of signature of proc, not the order. i.e. match made based on number and type of arguments. 

Is there a way to make the first one work as is without having to worry about order? Or Am I making mistake somewhere?

restart;

interface(version);

`Standard Worksheet Interface, Maple 2025.1, Linux, June 12 2025 Build ID 1932578`

restart;

set_name :=overload(
        [     
            proc(first_name::string,last_name::string,$) option overload;
                print("in proc which takes two arguments");
            end,
       
            proc(the_name::string,$) option overload;
                 print("in proc which takes one argument");
            end          
        ]
    ):

set_name("me"); #why this call the first proc and not the second?

"in proc which takes two arguments"

set_name("me","joe");

"in proc which takes two arguments"

restart;

#change order of procs in list
set_name :=overload(
        [            
            proc(the_name::string,$) option overload;
                 print("in proc which takes one argument");
            end,      

            proc(first_name::string,last_name::string,$) option overload;
                print("in proc which takes two arguments");
            end       
        ]
    ):

set_name("me");

"in proc which takes one argument"

set_name("me","joe");

"in proc which takes two arguments"

 

 

Download why_order_makes_differenence_in_overload_nov_1_2025.mw

Please Wait...