Vorg van Geir
Vorg van Geir

Reputation: 682

Map entries become vectors when piped thru a macro

In Clojure, a map entry created within a macro is preserved...

(class (eval `(new clojure.lang.MapEntry :a 7)))
;=> clojure.lang.MapEntry

...but when piped thru from the outside context collapses to a vector...

(class (eval `~(new clojure.lang.MapEntry :a 7)))
;=> clojure.lang.PersistentVector

This behavior is defined inside LispReader.syntaxQuote(Object form) condition if(form instanceof IPersistentCollection).

Does anyone know if this is intended behavior or something that will be fixed?

Upvotes: 0

Views: 107

Answers (1)

yonki
yonki

Reputation: 583

If you want to understand this behavior you need to dive into construction of Clojure sequences and collections.

In fact, every Clojure map is, underneath, a sequence of vectors. Each [:key :val] pair is stored as two elements vector.

Have a proper look, you're asking for class of MapEntry, which is just a vector! Instead, Clojure class for maps is clojure.lang.PersistentArrayMap or clojure.lang.IPersistentMap. MapEntry is just one element, one part of whole map. And, as I said, because each entry in a Clojure map is really a vector, class of evaluated MapEntry is vector, as it should be.

Hope my explanation is understandable.

Upvotes: 2

Related Questions