In clojure, why is the type of an empty list different from the type of non-empty lists?

I want to judge if two values ​​have the same type, but I found that the empty list type is clojure.lang.PersistentList$EmptyList , not clojure.lang.PersistentList .

 user=> (def la '()) #'user/la user=> (def lb '(1 2)) #'user/lb user=> (def t (map type [la lb])) #'user/t user=> t (clojure.lang.PersistentList$EmptyList clojure.lang.PersistentList) user=> (apply = t) false user=> 

So, I am wondering why the type of an empty list is different from the type of a non-empty list and what is the right way to say if two things are of the same type?

+4
source share
1 answer

Do not rely on specific types of Clojure data structures. They are undocumented implementation details, and you have no guarantee that they will not change in future versions of Clojure.

it is much safer to rely on abstractions (for example, as defined by the IPersistentList or ISeq ). They are much less likely to change in ways that can break your code (I understand that Rich Hickey is very great for backward compatibility when it comes to abstractions. If you are dependent on a particular implementation, I think he will say it with his own mistake if all will break)

But even better , you should use functions in clojure.core like seq? or list? , depending on what exactly you want to find. Not only can they maintain backward compatibility for a long time, they also have the ability to work correctly with versions other than the JVM Clojure (e.g. ClojureScript).

+10
source

All Articles