Created
December 29, 2021 18:56
-
-
Save juan-reynoso/a6ce52e2d9569abc7e12f92224499dc6 to your computer and use it in GitHub Desktop.
Generic functions
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
;;; 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