Skip to content

Instantly share code, notes, and snippets.

@luksamuk
Created May 17, 2019 14:21
Show Gist options
  • Save luksamuk/9ab4ab7b9f348b35cd321217d42c0fdb to your computer and use it in GitHub Desktop.
Save luksamuk/9ab4ab7b9f348b35cd321217d42c0fdb to your computer and use it in GitHub Desktop.
Testing Common Lisp Object System's method dispatch
(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