SML# - Resources/ProgrammingExamples/Objects/Methods Diff

  • Added parts are displayed like this.
  • Deleted parts are displayed like this.

  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})
     }
  
    val q1 =
        ref
        {
         X = 0.0,
         Y = 0.0
         };
    val cq1 =
        ref
        {
         X = 0.0,
         Y = 0.0,
         Color = "Red"
         };
  
    #setX methods q1 1.0;
    #getX methods q1;
    fun moveX p (dx:real) =
      (#setX methods p (#getX methods p + dx));
    fun moveY p (dy:real) =
      (#setY methods p (#getY methods p + dy));
    fun move p (dx,dy) =
      (moveX p dx;
       moveY p dy);
     move q1 (10.0,20.0);
     #getX methods q1;
     #getY methods q1;
     move cq1 (10.0,20.0);
     #getX methods cq1;
     #getY methods cq1;