Created
May 17, 2019 14:21
-
-
Save luksamuk/9ab4ab7b9f348b35cd321217d42c0fdb to your computer and use it in GitHub Desktop.
Testing Common Lisp Object System's method dispatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defgeneric thing (x y)) | |
(defclass foo () ()) | |
;; BAR inherits FOO | |
(defclass bar (foo) ()) | |
(defmethod thing ((x foo) (y foo)) | |
(format t "Both are of type FOO~%")) | |
(defmethod thing ((x bar) (y foo)) | |
(format t "X is a BAR, Y is a FOO. Next...~%") | |
(call-next-method)) | |
(defmethod thing ((x foo) (y bar)) | |
(format t "X is a FOO, Y is a BAR. Next...~%") | |
(call-next-method)) | |
(defun test-dispatch () | |
(let ((x (make-instance 'bar)) | |
(y (make-instance 'bar))) | |
(thing x y))) | |
;;; Output: | |
;;; X is a BAR, Y is a FOO. Next... | |
;;; X is a FOO, Y is a BAR. Next... | |
;;; Both are of type FOO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment