Prototype-based Programming Languages
Traditional class-based OO languages are based on a deep-rooted duality:
- Classes define the basic qualities and behaviours of objects.
- Object instances are particular manifestations of a class.
For example, suppose objects of the Vehicle
class have a name and the ability to perform various actions, such as drive to work and deliver construction materials. Bob's car
is a particular object (instance) of the class Vehicle
, with the name "Bob's car". In theory one can then send a message to Bob's car
, telling it to deliver construction materials.
This example shows one of the problems with this approach: Bob's car, which happens to be a sports car, is not able to carry and deliver construction materials (in any meaningful sense), but this is a capability that Vehicle
s are modelled to have. A more useful model arises from the use of subclassing to create specializations of Vehicle
; for example Sports Car
and Flatbed Truck
. Only objects of the class Flatbed Truck
need provide a mechanism to deliver construction materials; sports cars, which are ill suited to that sort of work, need only drive fast. However, this deeper model requires more insight during design, insight that may only come to light as problems arise.
This issue is one of the motivating factors behind prototypes. Unless one can predict with certainty what qualities a set of objects and classes will have in the distant future, one cannot design a class hierarchy properly. All too often the program would eventually need added behaviours, and sections of the system would need to be re-designed (or refactored) to break out the objects in a different way. Experience with early OO languages like Smalltalk showed that this sort of issue came up again and again. Systems would tend to grow to a point and then become very rigid, as the basic classes deep below the programmer's code grew to be simply "wrong". Without some way to easily change the original class, serious problems could arise.
Dynamic languages such as Smalltalk allowed for this sort of change via well-known methods in the classes; by changing the class, the objects based on it would change their behaviour. However, such changes had to be done very carefully, as other objects based on the same class might be expecting this "wrong" behavior: "wrong" is often dependent on the context. (This is one form of the fragile base class problem.) Further, in languages like C++, where subclasses can be compiled separately from superclasses, a change to a superclass can actually break precompiled subclass methods. (This is another form of the fragile base class problem, and also one form of the fragile binary interface problem.)
In Self, and other prototype-based languages, the duality between classes and object instances is eliminated.
Instead of having an "instance" of an object that is based on some "class", in Self one makes a copy of an existing object, and changes it. So Bob's car
would be created by making a copy of an existing "Vehicle" object, and then adding the drive fast method, modelling the fact that it happens to be a Porsche 911. Basic objects that are used primarily to make copies are known as prototypes. This technique is claimed to greatly simplify dynamism. If an existing object (or set of objects) proves to be an inadequate model, a programmer may simply create a modified object with the correct behavior, and use that instead. Code which uses the existing objects is not changed.
Read more about this topic: Self (programming Language)
Famous quotes containing the words programming and/or languages:
“If there is a price to pay for the privilege of spending the early years of child rearing in the drivers seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.”
—Melinda M. Marshall (20th century)
“The less sophisticated of my forbears avoided foreigners at all costs, for the very good reason that, in their circles, speaking in tongues was commonly a prelude to snake handling. The more tolerant among us regarded foreign languages as a kind of speech impediment that could be overcome by willpower.”
—Barbara Ehrenreich (b. 1941)