I see this behavior in Clojure 1.2.1:
user=> (type '(1 2))
clojure.lang.PersistentList
user=> (type `(1 2)) ;; notice syntax-quote
clojure.lang.Cons
user=> (type '(1))
clojure.lang.PersistentList
user=> (type `(1))
clojure.lang.PersistentList
I expected `(1) to be a Cons just like `(1 2) is.
I also tried:
user=> (type (cons 1 nil))
clojure.lang.PersistentList
user=> (type (cons 1 `()))
clojure.lang.Cons
user=> (type (cons 1 '()))
clojure.lang.Cons
user=> (type (cons 1 []))
clojure.lang.Cons
So what is the reason for `(1) and (cons 1 nil) to be PersistentLists?
Like amalloy says, you shouldn’t program against those exact types but against the
seqabstraction.However, I think I can take a guess at the reason. The Clojure forms that produce a
PersistentListultimately call RT.java, specifically thecons(Object x, Object coll)method. It begins with a pretty odd check:if(coll == null) return new PersistentList(x), after which it creates aConsobject if that check doesn’t pass. If you look at earlier versions of the code, you can find this:So in an earlier version of the function, the call was dispatched to the
consmethod of the second argument, so the case when the second argument wasnull(i.e.nilin Clojure) needed special handling. Later versions don’t do that dispatching (or actually do it but in a different way, presumably to support a larger variety of collection types), but the check has been retained since it does not break any correctly written code.