3

1. Summary

Is it possible to destructure an unordered plist in cl-loop's for-clauses?


2. Details

From the documentation I would have expected destructuring to be universal, e.g. I would have expected

for (a . b) = data

and

for (a &rest b) = data

to do the same thing, and

for (&key a b) = '(:a 1 :b 2 :c 3)

to effectively allow destructuring plists. This doesn't seem to be the case.


3. Example

Consider the snippet

(cl-loop for plist in '((:a 1 :b 2 :c 3) nil) 
         for (&key a b) = plist
         collect (vector a b))

I would have expected the result to be

([1 2] [nil nil])

and instead I get

([1 :b] [nil nil])

i.e. &key is treated as a variable name, not as an argument list keyword.

Drew
  • 75,699
  • 9
  • 109
  • 225
kdb
  • 1,561
  • 12
  • 21
  • I don't know why you'd expect that: the documentation says "similar in concept to argument lists", not "identical to" – rpluim May 13 '20 at 14:47
  • @rpluim Mostly for consistency. Why have different notions and implementations for the same thing in one package? It just would make sense to have consistent destructuring everywhere. – kdb May 13 '20 at 15:40
  • That part of doc (especially the link) is misleading, however, after that part, only the support of the plain list (including dotted list and nested list) is documented, it doesn't mention it supports destructuring the Common Lisp argument, unlike for example cl-destructuring-bind's doc. – xuchunyang May 13 '20 at 16:06
  • Bottom line: It isn't possible to get keyword destructuring. (Though in hindsight it anyway isn't really suitable for destructuring plists, as they have different behavior when it comes to unexpected symbols, and plists allow arbitrary symbols as keys, not just keyword symbols). – kdb May 13 '20 at 16:10

0 Answers0