A new datatype in EuLisp is called a class. An entity of that class
is called an instance. For example, 1
is an
instance of the class <integer>. Notice the angle brackets:
this is just a syntactic convention in EuLisp; all language-defined class names
begin with < and end with >. The class <integer> is an
instance of the class <class>, which, in turn, is an instance of
itself.
The fields in a class are called slots. Defining a class automatically defines reader and writer functions for each slot.
In the following section we will use the example of defining a rational number class to illustrate the various elements of class definition. The definition of <ratio> could look like this:
(defclass
class-name superclass-name
slot-descriptions class-options)
Where the items in italics have the following properties:
(
slot-1 ...
slot-n)
. Each slot-i is of the
form (
slot-name
keyword:
key-name
default:
expression
accessor:
identifier)
. In
our example we have
num
and
den
.
make
(the
general constructor function). In our example,
num:
and den:
.
0
for the numerator and 1
for
the denominator. This means that if we don't supply a numerator
and a denominator, we will get back 0/1
, which is
adequate as a representation of 0
.
(
identifier
exp)
, where exp results in
an instance of this class, returns the value of this slot, and that
((setter
identifier)
exp-1 exp-2)
, where
exp-1 results in an instance of this class, updates the
slot with the result of exp-2. In our example,
num
and den
, but note that
there is no requirement that they be the same as the slot name, it
just often makes sense that they are.
2/3
and the use of the reader and (via
setter
) writer functions to operate on it.
(
id key-1 ...
key-n)
, which will define a function
named id which will take n arguments
which will be used as the initial values of the slots with those
keywords. In our example, we define a constructor function with
the name ratio
which takes two arguments, the
first of which will be the numerator and the second the
denominator of the new rational. You can define more than one
constructor function for a class, for example:
int->ratio
which
takes an integer and returns the corresponding rational and a
function called reciprocal
which takes an integer
and returns 1 upon that integer.
ratiop
,
following the Lisp convention that functions returning boolean
values are suffixed with `p' to indicate predicate.