-
-
Save friedbrice/915addaf78b562c5038f12e6629c39f1 to your computer and use it in GitHub Desktop.
Static typable EP solution
This file contains hidden or 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
| (defpackage :fwoar.extend | |
| (:use :cl ) | |
| (:export )) | |
| (in-package :fwoar.extend) | |
| #| ------------- Begin Library ------------- |# | |
| (defclass animal () | |
| ()) | |
| (defclass operation () | |
| ()) | |
| (defgeneric visit (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 visit ((animal cat) (operation greet)) | |
| (format *standard-output* "the cat stands aloof~%")) | |
| (defmethod visit ((animal cat) (operation leave)) | |
| (format *standard-output* "the cat purs happily~%")) | |
| (defmethod visit ((animal dog) (operation greet)) | |
| (format *standard-output* "the dog wags its tail joyfully~%")) | |
| (defmethod visit ((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