Use pointer to emulate reference

In the Java programming language, if p is reference to an object, then q = p will make a reference to the same object, and statement like q.a = 0 will alter the status of p as well. This is not the case in Shang, as q = p will make a duplicate of p. If one wishes to emulate the behaviors of object references in Java, one can design a front end class which has an attribute that is a pointer pointing to the real data. Thus copies of the front end refer to the same data. For example
global.person_data = class
   public gender = "M" in {"F", "M"};
   public age = 1 in 1 : 150;
   public first = "Mark" in ~/[A-Za-z][A-Za-z]*/;
   public last = "Brown" in ~/[A-Za-z][A-Za-z]*/;
   new = (gender, age, first, last) -> ();
end

global.person_ref = class
   private data_p;

   auto gender = () -> (parent.data_p) >>. gender;
   auto age = () -> (parent.data_p) >>. age
   auto first = () -> (parent.data_p) >>. first
   auto last = () -> (parent.data_p) >>. last

   new = function (gender, age, first, last) -> ()
      data_p = newpointer(global.person_data.new(gender, age, first, last));
   end

   common set_gender = function x -> ()
                          (parent.data_p) >>. gender = x;
                       end

   common set_age = function x -> ()
                          (parent.data_p) >>. age = x;
                       end

   common set_first = function x -> ()
                          (parent.data_p) >>. first = x;
                       end
   common set_last = function x -> ()
                          (parent.data_p) >>. last = x;
                       end
end

p1 = person_ref.new("M",25,"Derek", "Burke");
p1.set_age(29);
Now p1 acts as a reference to the actual person data. Copies of p1 are like aliases of the same person. If a copy of p1 is updated, the original p1 will undergo the same change.
p2 = p1;
p2.set_age(35);
p2.age
    // ans is 35
p1.age
    // ans is 35 also



oz 2009-12-22