Skip to content

Instantly share code, notes, and snippets.

@spacebat
Created April 27, 2016 04:31
Show Gist options
  • Save spacebat/dbad3b50684b3a516071abb3757dc585 to your computer and use it in GitHub Desktop.
Save spacebat/dbad3b50684b3a516071abb3757dc585 to your computer and use it in GitHub Desktop.
CLOS append method combination example
;; An example of append method combination to form a projection of an object's slots
(defclass odour-mixin ()
((odour :initarg :odour :initform nil :reader odour)))
(defclass colour-mixin ()
((colour :initarg :colour :initform nil :reader colour)))
(defclass sound-mixin ()
((sound :initarg :sound :initform nil :reader sound)))
(defclass critter (sound-mixin colour-mixin odour-mixin)
())
(defmethod print-object ((critter critter) stream)
(print-unreadable-object (critter stream :type t)
(with-slots (odour colour sound) critter
(format stream "~A ~A ~A" odour colour sound))))
(defgeneric properties-in-darkness (thing)
(:method append ((thing odour-mixin))
`((:odour . ,(odour thing))))
(:method append ((thing sound-mixin))
`((:sound . ,(sound thing))))
(:method-combination append))
(defparameter *critter* (make-instance 'critter :sound "rasp" :odour "foetid" :colour "green"))
;; *CRITTER*
*critter*
;; #<CRITTER foetid green rasp>
(properties-in-darkness *critter*)
;; ((:SOUND . "rasp") (:ODOUR . "foetid"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment