Question: what is correct way to call a method in object from constructor?

Maple help does not have an example I could find showing the right way to call method, which is meant to be called only from the constructor the object, before the object is fully build.

Help page does not have such example.

This worksheet shows 3 possible ways to do this. All seem to work fine. I am sure one can come up with more variations.

The question is, which one of these calls is the "right" way?

The method called from the constructor in this example is called process_age().

If someone knows an official Maple documentation showing one example of calling  a proc from constructor that will also help.

Version 1

 

restart;

module person()
   option object;

   local _name::string;
   local _age::posint;

   export ModuleCopy::static := proc( _self, proto::person,
        name::string,age::posint,$)
        _name:=name;
        _age:=process_age(age);
   end proc;

   local process_age:=proc(age::posint,$)::posint;
         age+1;
   end proc;

   export get_age::static:=proc(_self,$)::posint;
          _age;
   end proc;
end module;

module person () local _name::string, _age::posint, process_age; option object; end module

o:=Object(person,"me",99);
o:-get_age()

module person () local _name::string, _age::posint, process_age; option object; end module

100

 

 

Version 2

 

restart;

module person()
   option object;

   local _name::string;
   local _age::posint;

   export ModuleCopy::static := proc( _self, proto::person,
        name::string,age::posint,$)
        _name:=name;
        _age:=process_age(age);
   end proc;

   local process_age::static:=proc(age::posint,$)::posint;
         age+1;
   end proc;

   export get_age::static:=proc(_self,$)::posint;
          _age;
   end proc;
end module;

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

 

o:=Object(person,"me",99);
o:-get_age()

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

100

 

 

Version 3

 

restart;

module person()
   option object;

   local _name::string;
   local _age::posint;

   export ModuleCopy::static := proc( _self, proto::person,
        name::string,age::posint,$)
        _name:=name;
        _age:=_self:-process_age(age);
   end proc;

   local process_age::static:=proc(_self,age::posint,$)::posint;
         age+1;
   end proc;

   export get_age::static:=proc(_self,$)::posint;
          _age;
   end proc;
end module;

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

 

 

o:=Object(person,"me",99);
o:-get_age()

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

100

 

 

Download correct_way_to_call_method_from_constructor_oct_17_2025.mw

Please Wait...