-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.lisp
87 lines (64 loc) · 2.96 KB
/
example.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#| Copyright 2021 Dmitrii Korobeinikov
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the “Software”), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |#
;; * Commentary
;; Example code for README.org. It's not loaded by the system, is meant for
;; demonstration of some concepts and contains deliberate errors.
;; ** Code
(in-package :slot-extra-options-tests)
;; ** Example
;; *** Metaclass
(eval-always
(def-extra-options-metaclass example-metaclass ()
((start :initform nil ; `:coalescence' is `replace-or-inherit' by default
:type integer)
(validates :coalescence bound-only-once
:type boolean))))
;; *** Class: `alpha'
;; Note: specifying a non-integer for `start' would yield a type error (if
;; safety is 3 on SBCL).
(defclass alpha ()
((zulu :initform 22
:start 55
:validates t))
(:metaclass example-metaclass))
;; not strictly necessary, this is automatically done when an object is instantiated:
(c2mop:ensure-finalized (find-class 'alpha))
(let ((zulu-definition (find 'zulu (c2mop:class-slots (find-class 'alpha))
:key #'c2mop:slot-definition-name)))
(values (slot-definition-start zulu-definition)
(slot-definition-validates zulu-definition)))
;; => 55, T
;; *** Class: `beta', which inherits from `alpha'
(defclass beta (alpha)
((zulu :start 66))
(:metaclass example-metaclass))
(c2mop:ensure-finalized (find-class 'beta))
(let ((zulu-definition (find 'zulu (c2mop:class-slots (find-class 'beta))
:key #'c2mop:slot-definition-name)))
(values (slot-definition-start zulu-definition)
(slot-definition-validates zulu-definition)))
;; => 66, T
;; *** Wrong types
(defclass wrong-types () ; ERROR! WON'T COMPILE!
((v :start nil
:validates 0))
(:metaclass example-metaclass))
;; *** Wrong `bound-only-once' redifinition
(defclass wrong-inheritence (alpha)
((zulu :validates nil))
(:metaclass example-metaclass))
;; `:validates' can't be overriden once bound
(make-instance 'wrong-inheritence) ; ERROR! WON'T COMPILE!