Slight improvement for slot accessors
This commit is contained in:
parent
36eeb6769c
commit
d02322ce73
2 changed files with 19 additions and 0 deletions
|
@ -68,6 +68,14 @@ And, of course, you can chain multiple slot accesses and method calls together:
|
||||||
;; => (slot-value (method-of-return-value ...) 'slot-of-return-value)
|
;; => (slot-value (method-of-return-value ...) 'slot-of-return-value)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**NEW:** You can also call slot accessors without wrapping them in a cons. (This
|
||||||
|
works for any method that takes no additional arguments, but that might cause
|
||||||
|
code readability issues.)
|
||||||
|
``` common-lisp
|
||||||
|
[object .slot-accessor]
|
||||||
|
;; => (slot-accessor object)
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
`objective-lisp` is public domain. You can do whatever you want with it. I don't
|
`objective-lisp` is public domain. You can do whatever you want with it. I don't
|
||||||
really care about credit, it's just a silly little thing I wrote in a few hours.
|
really care about credit, it's just a silly little thing I wrote in a few hours.
|
||||||
|
|
|
@ -43,6 +43,10 @@
|
||||||
"Check if x is a sexpr of the form (.method args...)"
|
"Check if x is a sexpr of the form (.method args...)"
|
||||||
(and (consp x) (consp (car x)) (eq (caar x) :method-call)))
|
(and (consp x) (consp (car x)) (eq (caar x) :method-call)))
|
||||||
|
|
||||||
|
(defun consless-method-call-p (x)
|
||||||
|
"Check if x is of the form .method (no arguments)"
|
||||||
|
(and (consp x) (eq (car x) :method-call)))
|
||||||
|
|
||||||
(defun destructure-construct (construct)
|
(defun destructure-construct (construct)
|
||||||
"Recursively destructure an objective-lisp construct."
|
"Recursively destructure an objective-lisp construct."
|
||||||
(cond
|
(cond
|
||||||
|
@ -57,6 +61,13 @@
|
||||||
(cons `(,(cdar method-call) ,(car construct) ,@(cdr method-call))
|
(cons `(,(cdar method-call) ,(car construct) ,@(cdr method-call))
|
||||||
(cddr construct)))
|
(cddr construct)))
|
||||||
))
|
))
|
||||||
|
; Consless method call [object .method]
|
||||||
|
; => [object (:method-call . method)]
|
||||||
|
((consless-method-call-p (cadr construct))
|
||||||
|
(destructure-construct
|
||||||
|
(cons `(,(cdadr construct) ,(car construct))
|
||||||
|
(cddr construct)))
|
||||||
|
)
|
||||||
; Slot access [object slot]
|
; Slot access [object slot]
|
||||||
(t
|
(t
|
||||||
(destructure-construct
|
(destructure-construct
|
||||||
|
|
Loading…
Reference in a new issue