Question: Overload Procedures with default values and mixed types problem

The overloaded  procedure here test returns based on 2 lists or 3 lists entered. The two list has a mixed input type with a default value. The default value of "a" can cause a problem if an explicit value in not entered for "a" in foo1. I not sure is the mixed input type is adding to the problem.
By changing the order of the procedures the problem is avoidable here. But this just a simple example. When there are 6 or so procedures it can be very difficult to select a correct ordering.

Is there a way around this, apart from don't have default values?

I could experiment with changing the input order in each proc but that would break up some logical input sequences on me.

restart

 

foo:=overload([
                        proc(P1::list,P2::list,a::algebraic:=4,$)
                         option overload;
                         print("2 lists");
                          end proc,

                       proc(P1::list,P2::list,P3::list,$)
                         option overload;
                         print("3 lists");
                          end proc
                       ]);

proc () option overload; [proc (P1::list, P2::list, a::algebraic := 4, ` $`) option overload; print("2 lists") end proc, proc (P1::list, P2::list, P3::list, ` $`) option overload; print("3 lists") end proc] end proc

(1)

foo([1,2],[3,4])

"2 lists"

(2)

foo([1,2],[3,4],[4,7])

"3 lists"

(3)

 

 

 

foo1:=overload([
                        

                       proc(P1::list,P2::list,P3::list,$)
                         option overload;
                         print("3 lists");
                          end proc,

                        proc(P1::list,P2::list,a::algebraic:=4,$)
                         option overload;
                         print("2 lists");
                          end proc
                       ]);

proc () option overload; [proc (P1::list, P2::list, P3::list, ` $`) option overload; print("3 lists") end proc, proc (P1::list, P2::list, a::algebraic := 4, ` $`) option overload; print("2 lists") end proc] end proc

(4)

foo1([1,2],[3,4]); #incorrect output

"3 lists"

(5)

foo1([1,2],[3,4],4)

"2 lists"

(6)

foo1([1,2],[3,4],[4,7])

"3 lists"

(7)

 

Download 2024-02-3_Q_Overload_proc_.mw

Please Wait...