dash.el icon indicating copy to clipboard operation
dash.el copied to clipboard

Extend -let for EIEIO object slots

Open alphapapa opened this issue 8 years ago • 3 comments

Would it be feasible to extend -let to support binding to object slots? So e.g. instead of

(let ((typers (oref room :typers))
      (name (oref room :room-name))
      (topic (oref room :topic)))
  body)

One could do:

(-let (((&obj :typers typers :room-name name :topic topic) room))
  body)

Thanks.

alphapapa avatar Nov 13 '17 06:11 alphapapa

Yes, I wanted to do this for a long time, and also for cl-defstruct (not sure how to do that though)

Fuco1 avatar Nov 13 '17 09:11 Fuco1

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)))

alphapapa avatar Nov 13 '17 11:11 alphapapa

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.

Fuco1 avatar Nov 13 '17 11:11 Fuco1