Fork me on GitHub
#clojure-spec
<
2017-12-19
>
falak19:12:20

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?)

guy19:12:55

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

falak19:12:58

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).

falak19:12:11

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

seancorfield20:12:21

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

seancorfield20:12:35

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

falak20:12:27

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.