Slight improvement for slot accessors

This commit is contained in:
~keith 2021-11-16 16:57:40 +00:00
parent 36eeb6769c
commit d02322ce73
Signed by: keith
GPG Key ID: 5BEBEEAB2C73D520
2 changed files with 19 additions and 0 deletions

View File

@ -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)
```
**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
`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.

View File

@ -43,6 +43,10 @@
"Check if x is a sexpr of the form (.method args...)"
(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)
"Recursively destructure an objective-lisp construct."
(cond
@ -57,6 +61,13 @@
(cons `(,(cdar method-call) ,(car construct) ,@(cdr method-call))
(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]
(t
(destructure-construct