Cover by Brian Carper, Christophe Grand, Chas Emerick

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Relying upon Assertions

Our HTML generation functions are decent, but someone somewhere will inevitably attempt to abuse the API. For example, the Clojure-standard representation of XML (as produced by clojure.xml) looks like this:

{:tag :a, :attrs {:href "http://clojure.org"}, :content ["Clojure"]}

That’s quite different than our vector-based HTML representation. Unsurprisingly, html will do odd things with this map, since it’s not at all what it’s expecting:

(html {:tag :a, :attrs {:href "http://clojure.org"}, :content ["Clojure"]})
;= "{:content [\"Clojure\"], :attrs {:href \"http://clojure.org\"}, :tag :a}"

Cripes, it returns a string! That’s appropriate insofar as we somewhat lazily use html recursively to process the contents of element vectors, but this particular result is clearly not useful if we imagine a user that (perhaps reasonably) thinks she might be able to serialize a clojure.xml data structure to an HTML string using our html function. This is a situation where we’d like to fail as quickly as possible.

Enter assertions. Assertions test a condition and throw an error if the condition is not true:

(defn attrs
  [attrs]
  (assert (or (map? attr-map)                                    1 (nil? attr-map)) "attr-map must be nil, or a map") (->> attrs (mapcat (fn [[k v]] [\space (name k) "=\"" v "\""])) (apply str))) (attrs "hi") ;= #<AssertionError java.lang.AssertionError: ;= Assert failed: attr-map must be nil, or ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required