Using generic Lisp custom type in defmethod

I would like to use a specific type as a specialized parameter for a parameter defmethod

. Motivation is readability and flexibility to change at a later stage. Something like:

(deftype foo () 'fixnum)

(defmethod bar ((x foo)) ...)

(defmethod baz ((x foo)) ...)

      

However, this doesn't work. CLtL2 says "Deftype of the form does not create any classes."

So, I have to write:

(defmethod bar ((x fixnum)) ...)

(defmethod baz ((x fixnum)) ...)

      

An alternative would be to define a class with a name foo

that is nothing more than a wrapper around it fixnum

, but wouldn't that be an unacceptable overhead for something as simple as fixnum

?

Is there a better way?

+3


source to share


2 answers


Methods are not type-specific, they are class-specific or EQL. This is in part because the object can be of many types (for example, integer 1 is FIXNUM, BIT, UNSIGNED-BYTE, etc.) and it is not clear how to decide the priority.



If you need fewer utility and custom abstractions for types, TYPECASE or ETYPECASE may perform better than generic functions and methods.

+6


source


The best solution I've been able to find is to use filtered submit .

Something like that:



(define-filtered-function bar (x)
  (:filters (:foo (typep x 'foo))))

(defmethod bar :filter :foo (x) ...)

      

However, I can resort to Xach's suggestion if the overhead for using this is too high.

0


source







All Articles