-
Notifications
You must be signed in to change notification settings - Fork 2
/
prototype-specializer.lisp
52 lines (45 loc) · 1.66 KB
/
prototype-specializer.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
;;;; prototype-specializer.lisp --- prototype specializer examples.
;;;;
;;;; Copyright (C) 2013, 2014 Christophe Rhodes
;;;;
;;;; Author: Christophe Rhodes <[email protected]>
(cl:defpackage #:prototype-specializer.example
(:use
#:cl
#:prototype-specializer))
(cl:in-package #:prototype-specializer.example)
(defmacro defpvar (name value)
`(eval-when (:compile-toplevel :load-toplevel :execute)
(let ((val ,value))
(setf (slot-value val 'prototype-specializer::name) ',name)
(defparameter ,name val))))
(defpvar /animal/ (clone /root/))
(defpvar /fish/ (clone /root/))
(defpvar /shark/ (clone /root/))
(defpvar /healthy-shark/ (clone /root/))
(defpvar /dying-shark/ (clone /root/))
(add-delegation /fish/ /animal/)
(add-delegation /shark/ /animal/)
(add-delegation /shark/ /healthy-shark/)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defgeneric fight (x y)
(:generic-function-class prototype-generic-function)))
(defmethod fight ((x /healthy-shark/) (y /shark/))
(remove-delegation x)
(add-delegation x /dying-shark/)
x)
(eval-when (:compile-toplevel :load-toplevel :execute)
(defgeneric encounter (x y)
(:generic-function-class prototype-generic-function)))
(defmethod encounter ((x /fish/) (y /healthy-shark/))
(format t "~&~A swims away~%" x))
(defmethod encounter ((x /fish/) (y /animal/))
x)
(defmethod encounter ((x /healthy-shark/) (y /fish/))
(format t "~&~A swallows ~A~%" x y))
(defmethod encounter ((x /healthy-shark/) (y /shark/))
(format t "~&~A fights ~A~%" x y)
(fight x y))
;; It is possible to use PROTOTYPE-OBJECTs (as opposed to their names)
;; #.`(defmethod encounter ((x /fish/) (y ,/animal/))
;; x)