Question: rules for calling object local method from constructor

For a module of type object, where the constructor needs to call helper function. This helper function will only be called from the constructor and no where else.

Should this local function be passed _self also and be static as well? I looked at help and could not find an example. All the examples there show methods which are export in the object, which means to be called from outside after the object is finished constructing.

I found that both versions work (using _self or not).

I asking because I am thinking that _self might not be ready to be used inside the constructor, since object is not yet done building.

Below is worksheet showing the two versions. Both work.

What are the rules to use for calling local module procs, which is part of object, but will be only called from inside the constructor and no where else?  

Should these local procs be static? should one also pass _self to it?

restart;

 

Version one

 

restart;

module person()
 option object;
 export name::string;
 export ID::integer;
 local  salary::posint;
 
 #constructor
 export ModuleCopy::static := proc( _self::person, proto::person, name::string, ID::integer, $ )
    #print("Initilizing object with with args: ", [args]);
    _self:-name:=name;
    _self:-ID:= ID;
    _self:-salary:= generate_salary(ID);
 end proc;

 export get_salary:=proc(_self::person,$)::posint;
     _self:-salary;
 end proc;

 #this will only be called by constructor
 local generate_salary::static:=proc(ID::integer,$)::posint;
    local roll;
    randomize():
    roll := rand(1..ID):
    roll();
 end proc;
end module;

module person () local salary::posint; export name::string, ID::integer, get_salary; option object; end module

o:=Object(person,"me",10):
o:-name;
o:-get_salary();

"me"

8

 

Version 2

 

restart;

module person()
 option object;
 export name::string;
 export ID::integer;
 local  salary::posint;
 
 #constructor
 export ModuleCopy::static := proc( _self::person, proto::person, name::string, ID::integer, $ )
    #print("Initilizing object with with args: ", [args]);
    _self:-name:=name;
    _self:-ID:= ID;
    generate_salary(_self);
 end proc;

 export get_salary:=proc(_self::person,$)::posint;
     _self:-salary;
 end proc;

 #this will only be called by constructor
 local generate_salary::static:=proc(_self::person,$)::posint;
    local roll;
    randomize():
    roll := rand(1.._self:-ID):
    _self:-salary:=roll();
 end proc;
end module:

o:=Object(person,"me",10):
o:-name;
o:-get_salary();

"me"

3

Download ex1.mw

Please Wait...