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

Proposed new type expression



Here is an extension to the Common Lisp type system that people might
want to keep in mind.  It didn't take me very long to implement this
(10 minutes plus about 3 hours of improvements to the type implementation
that I got stimulated to do while looking at it).

(DECLARE <type>) is a type expression that can be used for discrimination.
It treats <type> the way it would be treated for declaration, instead
of the normal way it is treated for discrimination.  For most types this
makes no difference, but for ARRAY and COMPLEX types, and types built on
them such as VECTOR, it does make a difference and furthermore it hides
implementation-dependent knowledge of what specialized types are provided
by the particular implementation.

<type> cannot be (FUNCTION ...) [although I think an implementation could
provide this as an extension if it has the ability to check arg/value types.]

Example of use:

  (defun foo (image)
    (declare (type (array (signed-byte 12)) image))
    (when *enable-type-checking*
      (check-type image (declare (array (signed-byte 12)))))
    ...)

In an implementation that has only the minimum specialized arrays required by
Common Lisp, (DECLARE (ARRAY (SIGNED-BYTE 12))) is equivalent to (ARRAY T)
while (ARRAY (SIGNED-BYTE 12)) is equivalent to NIL.  In another
implementation that has specialized arrays of 16-bit twos-complement integers,
(DECLARE (ARRAY (SIGNED-BYTE 12))) is equivalent to (ARRAY (SIGNED-BYTE 16))
but (ARRAY (SIGNED-BYTE 12)) is still equivalent to NIL.  In a third
implementation that has specialized arrays of 12-bit twos-complement integers,
(DECLARE (ARRAY (SIGNED-BYTE 12))) and (ARRAY (SIGNED-BYTE 12)) are
equivalent.