Previous section: The classes <function>, <generic-function>, and <method>

Dylan reference manual -- Introduction

7. Classes

Introduction

Classes are used to categorize Dylan objects. Classes specify the structure of their instances. In concert with methods, classes help determine the behavior of their instances. Every object is a direct instance of exactly one class.

A class determines which slots its instances have. Slots are the local storage available within instances. They are used to store the state of objects.

A class also helps determine the behavior of its instances. When an object is passed as an argument to a generic function, the generic function looks at the class (and perhaps identity) of the object to determine which method should be run.

Singletons are specializers used to indicate an individual object. A singleton for an object is created by passing the object to the function singleton. Singletons are used to customize the behavior of individual objects. Singletons can be used for specialization, like classes, but the specialization will only apply to the singleton object. When determining whether a method is applicable for a set of arguments, an argument with a singleton specializer must be == to the object used to create the singleton.

Slots and slot access

Slots correspond to the fields or instance variables of other object-oriented programming languages. For most slots, each instance of the class has private storage for the slot, so one instance can have one value in the slot and another instance can have another value.

Dylan follows the lead of some newer object-oriented languages by always accessing slots through function calls, rather than variable references.[22] In Dylan, slots are accessed through methods. The method that returns the value of a slot is called the getter method, and the method that sets the value of a slot is called the setter method. The getter and setter methods are added to generic functions. When defining a class, you specify slots by specifying to which generic functions the getter and setter methods should be added.

For example, the class definition for <point> might be

define class <point> (<object>)
  slot horizontal;
  slot vertical;
end class;
This definition indicates that instances of <point> should have two slots, horizontal and vertical. The getter method for the first slot is added to the generic function horizontal, and the getter method for the second slot is added to the generic function vertical. The setter method for the first slot is added to the generic function horizontal-setter, while the setter method for the second slot is added to the generic function vertical-setter.

To get the horizontal coordinate of a point, make the call

horizontal(my-point)
To set the horizontal coordinate to 10, use the corresponding setter function:
horizontal-setter(10, my-point)
or
horizontal(my-point) := 10;

Singleton types

Singletons provide a way to add methods to single instances. This lets programs specialize a single object, without changing other objects of the same class, and without defining a whole new class just for the object.

A singleton is a type, but it is not a class. It is little more than a pointer to a single object. The singleton's sole purpose is to indicate that object, so that a method can be created that specializes on the object. By defining a method that specializes on a singleton (rather than on a class), you have defined a method that discriminates on the singleton's object.

Singleton methods are considered more specific than methods defined on an object's original class. Singletons are the most specific specializer.

? define method double (thing :: singleton(#"cup"))
    #"pint"
  end method

? double (#"cup")
#"pint"
? double (10)
20
Dylan provides a shorthand for singletons used as method specializers. The folowing definition is equivalent to the one above.
? define method double (thing == #"cup")
    #"pint"
  end method

? double (#"cup")
#"pint"

Next section: Defining New Classes