Representing Simple Objects

 val methods =
   {
    getX = fn self => #X (!self),
    setX = fn self => fn x => self := (!self # {X = x}),
    getY = fn self => #Y (!self),
    setY = fn self => fn x => self := (!self # {Y = x}),
    getColor = fn self => #Color (!self),
    setColor = fn self => fn x => self := (!self # {Color = x})
    }

   local
     val state =
       ref
       {
        X = 0.0,
        Y = 0.0
        }
   in
     val p1 =
       fn method => method methods state
   end;
   p1 # setX 1.0;
   p1 # getX;

   local
     val state =
       ref
       {
        X = 0.0,
        Y = 0.0,
        Color = "Green"
        }
   in
     val cp1 =
       fn method => method methods state
   end;
   cp1 # setX 1.0;
   cp1 # getX;

   p1 #getX;
   p1 #getY;
   cp1 #getX;
   cp1 #getY;
Last modified:2007/03/30 10:40:48
Keyword(s):
References:[Record Polymorphsim and Objects]