Question: passing the object itself to one of its method causes a problem. Why?

As I was trying things with Maple OOP, I noticed strange behavior which I do not understand. I'll explain the problem in words first then given an example.

I have Person class. Then made one instance of it. In this class, there is one method which is defined to take in an object of same type as the class itself.

I found when I pass the object itself to its own method, it gives an error that the method expects a second argument of type person which is missing. But it is not missing.

Then when creating a second object of same type, and passing the second object, it worked! 

Why?  Here is MWE. Attached worksheet.  I would have expected same behavior in both cases. EIther both work, or both do not work. As as long as the object being passed is the correct type (person).

It seems in the first case, Maple noticed the object being passed to the method happened to be same object where this method is, and it did not pass it. Hence the missing second argument.  Is this a documented behaviour somewhere? (I made sure to make all method static though, so same code is used by all instances of the class. This is strange error message).


`Standard Worksheet Interface, Maple 2022.1, Windows 10, May 26 2022 Build ID 1619613`


    option object;
    export name::string:="";  

    #--- constructor---
    export ModuleCopy::static:=proc(_self,proto, name::string, $)
      _self:-name := name;
    end proc;

    export process::static:=proc(_self, the_input::person ,$)
       print("in person::process. name = ", the_input:-name);
    end proc;

end module;




p:-process(p);  #why this fail

Error, invalid input: process uses a 2nd argument, the_input (of type person), which is missing

o:=Object(person,"new_name"); #make new object and try again


p:-process(o); #why this OK?

"in person::process. name = ", "new_name"





Ok, I am all set. I found a good way to do this. i.e. pass the object to one of its method. One has to make a copy of the object first and pass the copy. Can't use the same object. This is how I do it now. 

    option object;
    export name::string:="";  

     export ModuleCopy::static:= overload( 
         proc(_self,proto, $)option overload;
            print("Enter 2 args constructor of person");  
            #do nothing    
         end proc,

         proc(_self,proto, name::string, $) option overload; 
             print("Enter 3 args constructor of person");      
             _self:-name := name;
        end proc

    export process::static:=proc(_self, the_input::person,$)
       print("the_input:-name = ",the_input:-name);
    end proc;
end module; 

#p:-process(p);  #instead of this do the following

Which works and gives

              "Enter 3 args constructor of person"
                  p := Object<<2370600065568>>
              "Enter 2 args constructor of person"
                  o := Object<<2370600048288>>
                   "the_input:-name = ", "me"

So I am now able to pass the object (or rather copy of it) as explicit argument to other methods and other modules.  I am not sure why it does not work when using the object itself and had to make a copy. 

Please Wait...