From d02322ce73e6c5554d7a41046a5f1d4067d75494 Mon Sep 17 00:00:00 2001 From: ~keith Date: Tue, 16 Nov 2021 16:57:40 +0000 Subject: [PATCH] Slight improvement for slot accessors --- README.md | 8 ++++++++ objective-lisp.lisp | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 75a3560..25959f5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/objective-lisp.lisp b/objective-lisp.lisp index ef23141..92f50cc 100644 --- a/objective-lisp.lisp +++ b/objective-lisp.lisp @@ -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