SE701:multiple-dispatch.lisp

From Marks Wiki
Jump to navigation Jump to search
;; How CommonLisp multiple dispatch differs from Java's single dispatch
(defclass a () ())
(defclass sub-a (a) ())

(defclass b () ())
(defclass sub-b (b) ())

(defgeneric foo (a b))

(defmethod foo ((x a) (y b))
  (format nil "foo called on A and B~%"))

(defmethod foo ((x sub-a) (y b))
  (format nil "foo called on SUB-A and B~%"))

(defmethod foo ((x a) (y sub-b))
  (format nil "foo called on A and SUB-B~%"))

(defmethod foo ((x sub-a) (y sub-b))
  (format nil "foo called on SUB-A and SUB-B~%"))

(defparameter *just-a* (make-instance 'a))
(defparameter *just-b* (make-instance 'b))
(defparameter *sub-a* (make-instance 'sub-a))
(defparameter *sub-b* (make-instance 'sub-b)))