SE701:multiple-dispatch.lisp

From Marks Wiki
Revision as of 19:39, 15 February 2008 by Mark (talk | contribs) (New page: ;; 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...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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)))