Skip to content

Instantly share code, notes, and snippets.

@juan-reynoso
Created December 29, 2021 18:56
Show Gist options
  • Save juan-reynoso/a6ce52e2d9569abc7e12f92224499dc6 to your computer and use it in GitHub Desktop.
Save juan-reynoso/a6ce52e2d9569abc7e12f92224499dc6 to your computer and use it in GitHub Desktop.
Generic functions
;;; A generic function specifies only the interface.
;;; It performs a high-level operation.
;;; Generic functions are functions that behave differently
;;; depending on the type and identity of the arguments.
(defgeneric say-type-of-object (x)
(:documentation "It displays the type of an object."))
;;; The implementation of a generic function does not exist in one place; it
;;; is distributed across a set of methods.
(defmethod say-type-of-object ((x number))
(format t "~% ~a is a number." x))
(defmethod say-type-of-object ((x string))
(format t "~% ~a is a string." x))
(defmethod say-type-of-object ((x list))
(format t "~% ~a is a list." x))
(defmethod say-type-of-object ((x symbol))
(format t "~% ~a is a symbol." x))
(defclass person ()
((name :accessor name
:initarg :name)))
(defparameter *me* (make-instance 'person :name "Juan"))
(defmethod say-type-of-object ((x person))
(format t "~% ~a is a person." x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment