summaryrefslogtreecommitdiff
path: root/example.lisp
blob: e2e48d53a941efb4a5512dbd2b548509624a25e5 (plain)
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
(defpackage #:mulk.protocols-examples
  (:nicknames #:protocols-examples)
  (:use #:cl #:protocols))


(in-package #:mulk.protocols-examples)


(define-protocol printable ()
  ((print-object * stream)))

(define-protocol serialisable ()
  ((serialise * stream)))

(define-protocol additive ()
  ((add * *)
   (negate *)))

(define-protocol multiplicative ()
  ((multiply * *)
   (invert *)))

(define-protocol field (additive multiplicative) ())

(define-protocol serialisable-field (serialisable field) ())


(defgeneric serialise (x stream))
(defgeneric add (x y))
(defgeneric negate (x))
(defgeneric multiply (x stream))
(defgeneric invert (x))

(defclass a () ())

;; Note the style warnings signalled by the following.
(implement-protocols a (additive multiplicative serialisable)
  (defmethod add ((x a) (y a)))
  (defmethod negate ((x a)))
  (defmethod multiply ((x a) y)))

(print (conforms-to-p 'a 'additive))
(print (really-conforms-to-p 'a 'additive))
(print (conforms-to-p 'a 'multiplicative))
(print (really-conforms-to-p 'a 'multiplicative))
(print (conforms-to-p 'a 'printable))
(print (really-conforms-to-p 'a 'printable))

(implement-protocols a (printable))

(print (conforms-to-p 'a 'printable))
(print (really-conforms-to-p 'a 'printable))