Defining New Classes
The double example given in the Functions chapter showed how to define methods for classes that already exist. A large portion of Dylan programming consists of defining new classes. New classes are created with define class.
define [adjectives ] class class-name (superclasses ) [Definition] slot-spec1 ; slot-spec2 ... end [class] [class-name]
define class is used to create classes. define class defines the module variable class-name and creates a class to store in the variable. The variable is made read-only.Next section: Slot OptionsThe adjectives are words that describe features of the class. The permitted adjectives are sealed, open, primary, free, abstract, and concrete. See the Controlling Dynamism chapter for more information.
The new class inherits from the superclasses, which is a sequence of expressions separated by commas which evaluate to classes. The elements of the list are evaluated. The list cannot contain duplicates, and the class heterarchy cannot be circular--that is, a class cannot be its own direct or indirect superclass. At least one superclass must be specified.
In addition to inheriting slots from its superclasses, the new class is given a slot for each of the slot-spec arguments. In the simplest format, a slot-spec is just:
slot variable-name
A getter method is defined on the generic function name, and a setter function is defined on the generic function name-setter. The full syntax for slot-specs is given in the Slot Options section below. The full syntax allows many more options when defining slots.
The following definition creates a new class and stores it in the module variable <menu>. Instances of the class will have two slots. The first slot is read with the generic function title and set with the generic function title-setter. The second slot is read with the generic function action and set with the generic function action-setter.
define class <menu> (<object>) slot title; slot action; end class;The creation and initialization of instances is controlled by the generic functions initialize and make. See the section on instance creation for more details.Slot Uniqueness
The collection of all the getter and setter generic functions for slots specified in a class or inherited from its superclasses must not contain any duplicates. This implies that an inherited slot cannot be overridden.If a superclass is inherited through multiple paths, its slots are only counted once. For example, if class A has direct superclasses B and C, and both B and C have D as a direct superclass, A inherits from D both through B and through C, but D's slots are only counted once so this multiple inheritance does not by itself create any duplicates among the getters and setters.
Note that if two classes each specify a slot and the two slots have the same getter and/or setter generic function, these two classes are disjoint --they can never have a common subclass and no object can be an instance of both classes. The same is true if one slot's getter function is the other slot's setter function (this would also cause a parameter list congruency error).