Open alphapapa opened 6 years ago
Yes, I wanted to do this for a long time, and also for cl-defstruct
(not sure how to do that though)
I'm guessing one would just use:
(cl-struct-slot-value STRUCT-TYPE SLOT-NAME INST)
This function has a compiler macro ‘cl-struct-slot-value--inliner’.
Return the value of slot SLOT-NAME in INST of STRUCT-TYPE.
STRUCT and SLOT-NAME are symbols. INST is a structure instance.
instead of plist-get
, etc.
There's also this, from cl-macs.el
:
(pcase-defmacro cl-struct (type &rest fields)
"Pcase patterns to match cl-structs.
Elements of FIELDS can be of the form (NAME PAT) in which case the contents of
field NAME is matched against PAT, or they can be of the form NAME which
is a shorthand for (NAME NAME)."
(declare (debug (sexp &rest [&or (sexp pcase-PAT) sexp])))
`(and (pred (pcase--flip cl-typep ',type))
,@(mapcar
(lambda (field)
(let* ((name (if (consp field) (car field) field))
(pat (if (consp field) (cadr field) field)))
`(app ,(if (eq (cl-struct-sequence-type type) 'list)
`(nth ,(cl-struct-slot-offset type name))
`(pcase--flip aref ,(cl-struct-slot-offset type name)))
,pat)))
fields)))
Oh yes... I confused myself :D The thing I wasn't sure about was related to a different project. Pulling the values is of course probably quite easy.
Would it be feasible to extend
-let
to support binding to object slots? So e.g. instead ofOne could do:
Thanks.