parent
50fc14dc0b
commit
cb4f65c81b
@ -133,20 +133,20 @@ Now we can write functions for defining:
|
||||
- the constructor, with a function that takes a name,
|
||||
and a list of slots, and returns a form that will define
|
||||
the constructor when evaluated:
|
||||
```cl
|
||||
```cl
|
||||
(defun constructor (name slots)
|
||||
`(defun ,(constructor-name name) ,slots
|
||||
(list ,@slots)))
|
||||
```
|
||||
```
|
||||
- the slot accessors. This one will return a list of forms,
|
||||
that will each define an accessor for one of the slots.
|
||||
```cl
|
||||
```cl
|
||||
(defun accessors (name slots)
|
||||
(loop for slot in slots
|
||||
for i upfrom 0 collect
|
||||
`(defun ,(accessor-name name slot) (obj)
|
||||
(nth i obj))))
|
||||
```
|
||||
```
|
||||
- the slot setters. Note that these won't actually
|
||||
be used by the users of our library. In common lisp, we don't
|
||||
really use separate functions for setters. For example,
|
||||
@ -157,20 +157,20 @@ use `(setf (point-x my-point-object) some-value)` to set it to `some-value`.
|
||||
setter function. This provides a unified interface for accessing fields,
|
||||
no matter what the underlying implementation is. Anyway, here's
|
||||
my function for defining the setters:
|
||||
```cl
|
||||
```cl
|
||||
(defun setters (name slots)
|
||||
(loop for slot in slots
|
||||
for i upfrom 0 collect
|
||||
`(defun ,(setter-name name slot) (obj val)
|
||||
(setf (nth i obj) val))))
|
||||
```
|
||||
```
|
||||
- finally, the aforementioned `defsetf` forms:
|
||||
```cl
|
||||
```cl
|
||||
(defun setfers (name slots)
|
||||
(loop for slot in slots collect
|
||||
`(defsetf ,(accessor-name name slot)
|
||||
,(setter-name name slot))))
|
||||
```
|
||||
```
|
||||
|
||||
As Common Lisp is a highly interactive language, we can try each
|
||||
of these functions in the REPL with very little effort:
|
||||
|
Loading…
x
Reference in New Issue
Block a user