In Clojure, I find this surprising:
> (count nil)
0
I would expect a type error, as in this case:
> (count 77)
java.lang.UnsupportedOperationException: count not supported on this type: Long
since nil
is not a list:
> (list? nil)
false
Does nil
have a special status as an empty sequence?
From the official documentation:
count
:
Returns the number of items in the collection. (count nil) returns 0. Also works on strings, arrays, and Java Collections and Maps
So this is the spec ;)
I imagine that it ensures that any mutating value having a "countable" type can be handled at runtime.
Indeed, any reference, referencing an allowed type (strings, arrays, and Java Collections and Maps) might target nil
at some point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With