Question: how to use overload for an object method?

I can't figure the correct syntax for using overloaded proc name as an exported method for an object. Maple gives

Error, static procedure `person:-set_name` refers to non-static local or export `person:-_name::string` in surrounding scope

Tried different places of where to put the ::static spec but none worked.

Here is a version which does not use overload which works. But I wanted to merge the two procs called set_name_one() and set_name_two() shown below into one called set_name().

restart;

module person()
  option object;
  local _name::string;
  export ModuleCopy::static := proc(_self,proto::person,the_name::string)
        _name:=the_name;
  end proc;

  export set_name_two::static:= proc(_self,first_name::string,last_name::string,$)
       print("in proc which takes two arguments");
       _name:=cat(first_name," ",last_name);
  end proc;

  export set_name_one::static:= proc(_self,the_name::string,$)
       print("in proc which takes one argument");
       _name:=the_name;
  end proc;

  export get_name::static:=proc(_self,$)
        _name;
  end proc;
end module;

module person () local _name::string; option object; end module

o:=Object(person,"me")

module person () local _name::string; option object; end module

o:-set_name_two("me","joe");

"in proc which takes two arguments"

"me joe"

o:-set_name_one("me");

"in proc which takes one argument"

"me"

 

Download working_version_OOP_nov_1_2025.mw

 

This below now uses overload. It seems inside the overloaded proc, one can't refere to _self local methods such as _name in this example?

interface(version);

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

restart;

module person()
  option object;
  local _name::string;
   export ModuleCopy::static := proc(_self,proto::person,the_name::string)
        _name:=the_name;
    end proc;

    export set_name::static := overload(
        [
             proc(_self,first_name::string,last_name::string,$) option overload;
                last_name;
                print("in proc which takes two arguments");
                _name:=cat(first_name," ",last_name);
            end,

            proc(_self,the_name::string,$) option overload;
                  print("in proc which takes one argument");
                  _name:=the_name;
            end
        ]
    );

    export get_name::static:=proc(_self,$)
        _name;
    end proc;
end module;

  

module person () local _name::string; option object; end module

o:=Object(person,"me")

Error, static procedure `person:-set_name` refers to non-static local or export `person:-_name::string` in surrounding scope

 

 

Download overload_in_OOP_nov_1_2025.mw

Next, I tried without using static:: on the overload. Now the initial error is gone, But the method call to set_name() gives an error. (see below)

Is there something I am doing wrong? Or overload does not work for object methods? Is there a workaround?

interface(version);

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

restart;

module person()
  option object;
  local _name::string;
   export ModuleCopy::static := proc(_self,proto::person,the_name::string)
        _name:=the_name;
    end proc;

    export set_name := overload(
        [
             proc(_self,first_name::string,last_name::string,$) option overload;
                last_name;
                 print("in proc which takes two arguments");
                _name:=cat(first_name," ",last_name);
            end,

            proc(_self,the_name::string,$) option overload;
                   print("in proc which takes one argument");
                  _name:=the_name;
            end
        ]
    );

    export get_name::static:=proc(_self,$)
        _name;
    end proc;
end module;

  

module person () local _name::string; export set_name; option object; end module

o:=Object(person,"me")

module person () local _name::string; export set_name; option object; end module

o:-set_name("me");

"in proc which takes one argument"

Error, invalid input: no implementation of set_name matches the arguments in call, 'set_name("me")'

 

 

Download overload_in_OOP_nov_1_2025_V2.mw

Here is another version where static:: is kept on the overload, but now code changed to use _self:-_name to refer to the private object variable instead of just _name (even though this is not needed). Now the initial error is gone, but now calling object:-set_name("me") result in new error Error, (in person:-set_name) `me` does not evaluate to a module

interface(version);

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

restart;

module person()
  option object;
  local _name::string;
   export ModuleCopy::static := proc(_self,proto::person,the_name::string)
        _name:=the_name;
    end proc;

    export set_name::static := overload(
        [
             proc(_self,first_name::string,last_name::string,$) option overload;
                last_name;
                print("in proc which takes two arguments");
                _self:-_name:=cat(first_name," ",last_name);
            end,

            proc(_self,the_name::string,$) option overload;               
                  print("in proc which takes one argument");
                  _self:-_name:=the_name;
            end
        ]
    );

    export get_name::static:=proc(_self,$)
        _name;
    end proc;
end module;

  

module person () local _name::string; option object; end module

o:=Object(person,"me")

module person () local _name::string; option object; end module

o:-set_name("me");

"in proc which takes one argument"

Error, (in person:-set_name) `me` does not evaluate to a module

 

 

Download overload_in_OOP_nov_1_2025_V3.mw

I am starting to think overload does not work for object procs but only for standard procs or for standard module procs which is does not have option object;  That will be too bad.

I wonder if there is alternative to overload for Maple object if this was the case.

 

Please Wait...