Fork me on GitHub
#clojure-spec
<
2016-10-17
>
akiel10:10:13

Hi. Is there something like s/assert which is executed every time? I like to reserve s/assert for checks I want potentially disable in production. But I have some checks I like to execute in all cases.

Niclas11:10:55

How would one go about specing a function that takes a callback that may take any kind of argument? For ex:

(defn myfunc [k cb]
  (do-something-async k (fn [res err]
                          (check-err err)
                          (cb res err))))

akiel11:10:59

Why takes the callback any kind of argument?

Niclas11:10:35

If do-something-async in this example is an un-speced library function where I only care about specing functions in my project and not external ones

akiel11:10:25

But if you know what cb should look like, it would be beneficial to fail fast at your function.

Alex Miller (Clojure team)12:10:00

You can use fspec to spec a function arg and any? to spec an arg that takes any value

Alex Miller (Clojure team)12:10:41

(s/fdef myfunc :args (s/cat :k keyword? :cb (s/fspec :args (s/cat :res any? :err any?))))

Niclas15:10:50

@alexmiller Thanks, that’s what I was looking for!

vikeri15:10:37

Should I be able to spec an infinite sequence as an argument to a function? I tried s/every but it got stuck in an infinite loop.

Alex Miller (Clojure team)16:10:15

s/every samples so that should be able to work. Would be interested in seeing more.

Alex Miller (Clojure team)16:10:49

(s/valid? (s/every int?) (range)) ;; => true

akhudek20:10:27

Is there a recommended way to handling function arguments like [a b & [foo]] ?

akhudek20:10:50

I suppose you could do (s/or :two-args … :three-args ..)

arohner21:10:37

also (s/cat :a a :b b :foo (s/? foo?))

arohner21:10:46

the choice partly depends on intent

akhudek23:10:19

ah, interesting thanks