10.5 Allocation of slots

Each slot has a particular kind of allocation. The allocation of a slot determines where the storage for the slot's value is allocated, and it determines which instances share the value of the slot. There are four kinds of allocation:

Instance

Each instance allocates storage for the slot, and each instance of the class that defines the slot has its own value for the slot. Changing a slot in one instance does not affect the value of the same slot in a different instance. Instance allocation is the default, and is the most commonly used kind of allocation.

Virtual

No storage is allocated for the slot. You must provide a getter method that computes the value of the virtual slot. See Section 10.6.

Class

The class that defines the slot allocates storage for the slot. Instances of the class that defines the slot and instances of all that class's subclasses see the same value for the slot. That is, all general instances of the class share the value for the slot.

Each-subclass

The class that defines the slot and each of its subclasses allocate storage for the slot. Thus, if the class that defines the slot has four subclasses, the slot is allocated in five places. All the direct instances of each class share a value for the slot.

We can give an example of an each-subclass slot by defining a <vehicle> class:

define class <vehicle> (<physical-object>)
  // Every vehicle has a unique identification code
  slot vehicle-id :: <string>, required-init-keyword: id:;
  // The normal operating speed of this class of vehicle
  each-subclass slot cruising-speed :: <integer>;
end class <vehicle>;

The slot cruising-speed is defined with the each-subclass slot allocation. We use each-subclass allocation to express that, for example, all instances of Boeing 747 aircraft share a particular cruising speed, and all instances of McDonnell Douglas MD-80 aircraft share a particular cruising speed, but the cruising speed of 747s does not need to be the same as the cruising speeds of MD-80s.