[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Compile-time enforcement of Common Lisp type declarations

    Date: Wed, 1 Mar 89 10:04:14 PDT
    From: gyro@kestrel.arpa (Scott B. Layson)

    However, type checking is not done for DEFSTRUCT slots, at either
    compile or run time.  (I tried it.)

It would not be correct to complain about the type of the default value
of a slot at compile time.  CLtL doesn't actually say this, but it came
up in X3J13 and the ANSI CL spec will make this clear.

The reasons is that the syntax of DEFSTRUCT sometimes forces you to
include a default value even when you never plan on letting it default.
The syntax of slot descriptions is (NAME &optional DEFAULT &rest KEYS),
so you must supply a default if you want to specify any keywords.  If
you don't actually intend to make use of the defaulting you're likely to
want to put a default of NIL there, even though this might not conform
to the :TYPE you specify for the slot.

There currently are some CL implementations that do produce a warning in
such cases.  In order to shut them up you either need to change the
:TYPE specification to something like (OR NULL type) or change the
default to a call to a function that can return an object of the
appropriate type.  The first is less correct because it might result in
a less efficient implementation of the structure or suppress a potential
runtime error (since it makes (setf (...) nil) valid when it previously
wasn't); the second is likely to be very verbose and/or confusing.
Prohibiting implementations from warning was chosen as the preferable

Note that an implementation MAY warn at compile time if it sees a
constructor function being called without specifying an explicit value
for a slot whose default is not of the declared type.