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

Confusion about deftype and CLOS classes.

I need a bit of help figuring out something unclear in CLtL2, and/or
maybe a MCL2.0 bug.  I do the following:

(deftype foo () 'fixnum)

(defmethod zorch ((x foo)) ...)

and get the error
> Error: Argument foo is not of the required type.
> While executing: find-class

This seems consistent with the statement on p. 781 that "The form
deftype does not create any classes."  However, to be able to use
deftype as a means of abstracting from fixnum, I need to be able to
define a class for it.  So, I try

(setf (find-class 'foo) (find-class 'fixnum))

but alas get another error:
> Error: Cannot set (find-class 'foo) because type foo is already
defined by deftype
> While executing: ccl::set-find-class

I also can't just do
(defclass foo (fixnum) ())
because fixnum is s built-in class, and thus can't be a base class for

After an hour of playing around, I realized that the (setf ...) DOES
work if I simply don't do the (deftype ...) first!  Indeed, then
(typep 123 'foo) works, just as hoped for.  Is this correct CL
behavior?  I.e., are deftype and (setf (find-class ...) ...) simply
mutually exclusive?

In any case, this does not seem sufficient, however.  In another example,
I need to define

(deftype bar () (integer 0 *)), i.e., the non-negative integers.

Alas, I can't then see a way to use bar for method selection.  I.e., I
can't do (defmethod zorch ((x bar)) ...), though (typep x 'bar) works.
I realize that CLOS does not define classes for types specified by
type specifier lists such as (integer 0 *), but I can't even see how
to get method selection based on the "integer" part of the spec.
Have I simply run into an awkward corner of CLOS/type interaction, or
is there some trick I'm overlooking?  Do I need to do something ugly

(deftype bar () (integer 0 *))
(setf (find-class 'bar-class) (find-class 'integer))
(defmethod zorch ((x bar-class))
  (when (typep x 'bar) ...))

Any enlightenment would be much appreciated.  Thank you.  --Pete Szolovits