Fork me on GitHub
#beginners
<
2018-05-18
>
ramblurr10:05:35

Is there a spec regex function for or ?

ramblurr10:05:56

I want to specify that an arg should be ::foo-type or nil: :args (s/cat :arg1 (s/*or* ::foo-type nil?)

ramblurr10:05:54

googling for something with the keyword or is damn near impossible heh

troglotit10:05:33

there’s s/or and s/alt: they’re described better here https://clojure.org/guides/spec

ramblurr10:05:30

s/or doesn't work in the example I give above.

ramblurr10:05:19

if I pass a ::foo-type as arg1, then spec complains that it doesn't satisfy nil?

ramblurr10:05:45

I thought there would be a s/| to go along with s/&, but there isn't

ramblurr10:05:50

s/? is arity 1

troglotit10:05:50

you should provide keywords as described in guide (s/or :foo ::foo :nil nil?)

ramblurr10:05:01

oh interesting i see

ramblurr10:05:48

yea, thanks @troglotit this works: :args (s/cat :arg1 (s/or :foo ::foo-type :nil nil?)

ramblurr10:05:13

the extra keywords seem superfluous though 😕 they don't really refer to anything

troglotit10:05:06

I did hit that issue too 🙂. Seems like they’re required to provide context while conforming or explaining, and make decision based on that

ramblurr10:05:57

makes sense yes

alexmiller12:05:09

You should use s/alt in regex ops

alexmiller12:05:46

The keywords give names to the alternatives in the case of s/conform and context for errors via s/explain

seancorfield17:05:16

@ramblurr If you don't need to distinguish between ::foo-type and nil, you could just use (s/nilable ::foo-type)