defmethod with list type

Viewed 96

Is it possible in Common Lisp to define a method with a "list type"?

(defgeneric show (obj))

(defmethod show ((obj coordinate)) ;; Works
  ...)
(defmethod show ((obj [LIST OF coordinate])) ;; How to?
  ...)
1 Answers

You can write a method on list type but not on arbitrary type specifiers. From hyperspec:

If parameter-specializer-name is a symbol it names a class; if it is a list, it is of the form (eql eql-specializer-form).

Related question: Defmethod on Arbitrary Type Specifiers?

Something like this might work for you:

(defmethod show ((obj coordinate))
  ...)

(defmethod show ((obj null)))
(defmethod show ((obj cons))  ;; Or just loop it.
  (show (car obj))
  (show (cdr obj)))
Related