Question: overload vs. using optional arguments?

I have some of my main functions defined to take input in different formats. Some have required  parameters and also optional parameters. Similar to how say dsolve can take one ode, or a list of ode's or set of ode's and also other additional arguments.

Currently I use something like  ode::{`=`,set(`=`),list(`=`)} in the signature of the proc to indicate this input can be any of these types. There are also optional aguments.

So inside the proc, it does lots and lots of if this then do such else do such type of logic in order to determine which case/combination of input it is called with.

I am thinking of rewriting all of the API to use overload. Yes, it means I will have lots of copies of the same proc, each to handle specific case of the signature. But it also mean it will be now much simpler inside each of the overloaded proc's to determine which case of call it is processing.

I'd like to ask, is there anything to be aware of before making this change? does it affect performance much? It seems to me it will make the logic and the program simpler.  Here is a very simple example to illustrate.

Which you think is better? I like the overload version more. But I am worried that I will end up with too many versions of the API since I need one for each possible combination and may be this will slow Maple down? 

The following is the worksheet also.

restart;

interface(warnlevel=4);
kernelopts('assertlevel'=2):
foo:=overload(
[
   proc(A::`=`) option overload;
       print("single equation");
   end,
  
   proc(A::set(`=`)) option overload;
       if nops(A)=1 then
          print("single eq in a set");
       else
          print("more than one eq in a set");
       fi;
   end,

   proc(A::list(`=`)) option overload;
       if nops(A)=1 then
          print("single eq in a list");
       else
          print("more than one eq in a list");
       fi;
   end

]):

 

3

foo(x=1);
foo([x=1]);
foo([x=1,y=2]);
foo({x=1});
foo({x=1,y=2})

"single equation"

"single eq in a list"

"more than one eq in a list"

"single eq in a set"

"more than one eq in a set"

restart;

interface(warnlevel=4);
kernelopts('assertlevel'=2):

foo:=proc(A::{`=`,set(`=`), list(`=`)})

    if type(A,`=`) then
        print("single equation");
    elif type(A,set) then
          if nops(A)=1 then
             print("single eq in a set");
          else
             print("more than one eq in a set");
          fi;
    else #must be list
       if nops(A)=1 then
          print("single eq in a list");
       else
          print("more than one eq in a list");
       fi;
    fi;
end proc:

4

foo(x=1);
foo([x=1]);
foo([x=1,y=2]);
foo({x=1});
foo({x=1,y=2})

"single equation"

"single eq in a set"

"more than one eq in a set"

"single eq in a set"

"more than one eq in a set"

 

Download overload.mw

Please Wait...