magnars / dash.el

A modern list library for Emacs
GNU General Public License v3.0
1.66k stars 138 forks source link

Extend -let for EIEIO object slots #250

Open alphapapa opened 6 years ago

alphapapa commented 6 years ago

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.

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

alphapapa commented 6 years ago

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)))
Fuco1 commented 6 years ago

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.