Why? Why? Why?

One issue with more complex specs is that it’s not always immediately clear why a particular value doesn’t match. For example, you might have to look at this expression for a while to see why valid? returns false:

 (s/valid? ::book {:author :austen :title :emma})

To help you figure out why your spec isn’t matching, clojure.spec provides the explain function, which takes the same arguments as valid?. If the spec matches, explain will print a celebratory message, so that if you do this:

 (s/explain n-gt-10 44)

you will see this:

 Success!

Things are much more interesting if the spec doesn’t match. Do this:

 (s/explain n-gt-10 1)

and you will see something like the following:

 val​:​ 4 fails predicate​:​ (> % 10)

Thus we can use ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.