SML# - Resorces/ProgrammingExamples/Objects/Recods Diff

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

  val getX = fn self => #X (!self);
  val setX = fn self => fn x => self := (!self # {X = x});
  val getY = fn self => #Y (!self);
  val setY = fn self => fn x => self := (!self # {Y = x});
  val getColor = fn self => #Color (!self);
  val  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"
                  };
    fun moveX p (dx:real) =
      setX p (getX p + dx)
    fun moveY p (dy:real) =
      setY p (getY p + dy);
    fun move p (dx,dy) =
      (moveX p dx;
       moveY p dy);
     move q1 (10.0,20.0);
     getX q1;
     getY q1;
     move cq1 (10.0,20.0);
     getX cq1;
     getY cq1;