Fork me on GitHub

I am trying to write specs for a function whose argument can be nil or a collection. However, I am unable to combine those two using s/or. This is what I tried -

(s/fdef my-func
            :args (s/cat :arg1 (s/or :nil nil? :collection list?)))
This definitely won't work but it'd be great to have the option -
..... (s/or nil? list?)


can’t you do (s/def ::arg1 (s/nilable list?)) then just use that with s/cat :arg1 ::arg1 ?


Ah! I wasn't aware of s/nilable. Thanks! However, I am not sure why but (s/nilable coll?) works and s/nilable list? doesn't when the data structure is actually a list of maps (or nil).


When I run type on the args, it says clojure.lang.LazySeq


@fravani because list? is only true for things that implement IPersistentList -- which a lazy sequence does not.


But coll? is true for IPersistentCollection and LazySeq does implement that.


That makes sense. No wonder it worked when I converted the lazy seq to non-lazy using into () ... Thank you so much for the help.