Objects have slots, which resemble the data members found in most other object-oriented languages. Like variables, slots are bound to values; they don't actually contain their data. A simple Dylan class shows how slots are declared:
define class <vehicle> (<object>) slot serial-number; slot owner; end;
The above code would quick and convenient to write while building a prototype, but it could be improved. The slots have no types, and worse, they have no initial values. (That's no easy achievement in Dylan, to create an uninitialized variable!) The following snippet fixes both problems:
define class <vehicle> (<object>) slot serial-number :: <integer>, required-init-keyword: sn:; slot owner :: <string>, init-keyword: owner:, // optional init-value: "Northern Motors"; end class <vehicle>;
The type declarations work just like type declarations anywhere else in Dylan; they limit a binding to objects of a given class or of one of its subclasses, and they let the compiler optimize. The new keywords describe how the slots get their initial values. (The keyword init-function may also be used; it must be followed by a function with no arguments and the appropriate return type.)
To create a vehicle object using the new class declaration, a programmer could write one of the following:
make(<vehicle>, sn: 1000000) make(<vehicle>, sn: 2000000, owner: "Sal")
In the first example, make returns a vehicle with the specified serial number and the default owner. In the second example, make sets both slots using the keyword arguments.
Only one of required-init-keyword, init-value and init-function may be specified. However, init-keyword may be paired with either of the latter two if desired. More than one slot may be initialized by a given keyword.
Dylan also provides for the equivalent of C++ static members, plus several other useful allocation schemes. See the Dylan Reference Manual for the full specifications.