4

I noticed both pcase-let and cl-destructuring-bind seem to perform the same operation.

Is there any difference or reason to use one instead of the other?

eg:

(pcase-let ((`(,filename ,buf) (pop filename-and-buffer-list)))
  ;; do stuff.
  )

Seems to behave the same as:

(cl-destructuring-bind (filename buf) (pop filename-and-buffer-list)
  ;; do stuff.
  )
Drew
  • 75,699
  • 9
  • 109
  • 225
ideasman42
  • 8,375
  • 1
  • 28
  • 105
  • 3
    Nothing more than personal preference, IMO. `destructuring-bind` has been around much longer than `pcase`, and personally I find `pcase` forms harder to read; but YMMV. I'd just say use whichever one you like best. – phils Dec 21 '19 at 14:09
  • What @phils said. (And `cl-destructuring-bind` is a Common Lisp emulation, as you probably know.) – Drew Dec 21 '19 at 15:17

1 Answers1

8

cl-destructuring-bind was designed more or less specifically to destructure data made of cons cells. pcase-let on the other hand is just a special case of pcase which was designed to handle arbitrary data and be extensible (and be able to discriminate rather than only destructure).

So cl-destructuring-bind has a slightly more concise syntax for the simple cases like the one you show, whereas pcase-let imposes a more verbose syntax in exchange for the ability to destructure other data types such as structs, hash-tables, ...

Another difference is that pcase-let allows you to perform several such bindings, as in

(pcase-let ((PAT1 EXP1)
            (PAT2 EXP2)
            ...)
  ..)

whereas cl-destructuring-bind focuses on the single-pattern case, so you'd have to use

(cl-destructuring-bind PAT1 EXP1
  (cl-destructuring-bind PAT2 EXP2
    ...))

which again makes cl-destructuring-bind less verbose in the simple case.

As the designer&implementer of pcase I consider that the added verbosity cost of pcase-lets generality is minor (compared to the advantage of having a single syntax of patterns that works both for simple and complex cases).

Stefan
  • 26,154
  • 3
  • 46
  • 84