Guides | Language > OOP


the ability of different classes to respond to a message in different ways


Polymorphism is the ability of different classes to respond to a message in different ways. A message generally has some underlying meaning and it is the responsibility of each class to respond in a way appropriate to that meaning.

For example, the value message means "give me the effective value of this object".

The value method is implemented by these classes (among others):

Function :
Object :
Ref :

Let's look at how these classes implement the value message.


Here's the value method in class Object :

It simply returns itself. Since all classes inherit from class Object this means that unless a class overrides value, the object will respond to value by returning itself.


In class Function the value method is a primitive:

_FunctionValue is a C code primitive, so it is not possible to know just by looking at it what it does. However what it does is to evaluate the function and return the result.


The Ref class provides a way to create an indirect reference to an object. It can be used to pass a value by reference. Ref objects have a single instance variable called value. The value method returns the value of the instance variable value. Here is a part of the class definition for Ref:

Here is how it responds :

Ref also implements a message called dereference which is another good example of polymorphism. As implemented in Ref, dereference just returns the value instance variable which is no different than what the value method does. So what is the need for it? That is explained by how other classes respond to dereference. The dereference message means "remove any Ref that contains you". In class Object dereference returns the object itself, again just like the value message. The difference is that no other classes override this method. So that dereference of a Function is still the Function.

Object :
Ref :


Yet another example of polymorphism is play. Many different kinds of objects know how to play themselves.






Polymorphism allows you to write code that does not assume anything about the implementation of an object, but rather asks the object to "do what I mean" and have the object respond appropriately.