Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Forked from fiddlerwoaroof/extend.js
Last active June 4, 2019 17:13
Show Gist options
  • Select an option

  • Save friedbrice/915addaf78b562c5038f12e6629c39f1 to your computer and use it in GitHub Desktop.

Select an option

Save friedbrice/915addaf78b562c5038f12e6629c39f1 to your computer and use it in GitHub Desktop.
Static typable EP solution
(defpackage :fwoar.extend
(:use :cl )
(:export ))
(in-package :fwoar.extend)
#| ------------- Begin Library ------------- |#
(defclass animal ()
())
(defclass operation ()
())
(defgeneric operate (animal operation)
#| This method ensures type safety: as long as we're given something in the domain of the function
(animal, operation) => effect}, we'll get a result. |#
(:method ((animal animal) (operation operation))
(format *standard-output* "the animal is unresponsive~%")))
(defclass turtle (animal)
())
#| -------------- End Library -------------- |#
(defclass cat (animal)
(#| cat-specific members here |#))
(defclass dog (animal)
(#| dog-specific members here |#))
(defclass greet (operation)
())
(defclass leave (operation)
())
(defmethod operate ((animal cat) (operation greet))
(format *standard-output* "the cat stands aloof~%"))
(defmethod operate ((animal cat) (operation leave))
(format *standard-output* "the cat purs happily~%"))
(defmethod operate ((animal dog) (operation greet))
(format *standard-output* "the dog wags its tail joyfully~%"))
(defmethod operate ((animal dog) (operation leave))
(format *standard-output* "the dog looks very sad~%"))
(defun main ()
(dolist (animal (list (make-instance 'cat)
(make-instance 'dog)
(make-instance 'turtle)))
(dolist (action (list (make-instance 'greet)
(make-instance 'leave)))
(visit animal action)))) #| ===>
FWOAR.EXTEND> (main)
the cat stands aloof
the cat purs happily
the dog wags its tail joyfully
the dog looks very sad
the animal is unresponsive
the animal is unresponsive
|#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment