Object-Based Languages

Partly from chapter 4 of Abadi and Cardelli's book A Theory of Objects (Springer 1996).

Generating Objects

type CellType {
   int val;
   int getVal();
   void setVal(int n);
}

CellType myCell = new Object {
   int val = 0;
   int getVal() { return val; }
   void setVal(int n) { val = n; }
}

Factory-Based Languages

CellType makeCell(Integer n) {
   return new Object {
      int val = n;
      int getVal() { return val; }
      void setVal(int n) { val = n; }
   }
}

Prototype-Based Languages

CellType myCell2 = clone(myCell);

myCell2.value = 10; // no impact on myCell.value
myCell2.setVal =
   function(int n) {
      if (0 <= val) this.val := n;
   }

Inheritance in Prototype-Based Languages

type MemCellType extends CellType {
   int cache;
   void restore();
}

Obtaining donor features

Explicit

Inherited features (methods and attributes) must be designated by the host.

Implicit

All features are automatically inherited by the host.

Incorporating attributes into the host

Embedding-Based

Embedding means the host contains a copy of the donor's attributes. This makes it easier to interpret self.

Implicitly specifying donor attributes

MemCellType myCell3 = new Object extends myCell {
   int cache;
   void setVal(int n) { // override
      this.cache = this.val;
      this.val = n;
   }
   void restore() {
      this.val = this.cache;
   }
}

Explicitly specifying donor attributes

MemCellType myCell4 = new Object {
   int val;
   int  cache;
   void setVal(int n) {
      this.cache = this.val;
      embed myCell.set(n); // this = this, not myCell
   }
   int getVal() {
      return embed myCell.getVal();
   }
   void restore() {
      this.val := this.cache;
   }
}

Delegation-Based

Delegation means the host forwards unrecognized messages to the donor.

MemCellType myCell3 = new Object childOf myCell {
   int cache;
   void setVal(int n) { // override
      this.cache = this.val;
      delegate myCell.setVal(n);
   }
   void restore() {
      this.val = this.cache;
   }
}