Search code examples
error-handlingclojure

Can I include a message in pre and postconditions


I have this (not (some #(= (:length %1) 0) %)) as a postcondition. Written like this it's pretty clear, but when this condition isn't met I get this:

Assert failed: (not (some (fn* [p1__17852#] (= (:length p1__17852#) 0)) %))

Which isn't very readable. Is there a way to define the message for a postcondition, or for a precondition?

Edit 1:

Following noahlz and noisesmiths suggestion, (but using an external named function):

(defn not-zero-length
  [evseq]
  (not (some (fn [item] (= (:length item) 0)) evseq)))

(defn my-func
  [evseq]
  {:post [(not-zero-length %)]}
  evseq)

(my-func '({:length 3}{:length 0}))

gives:

AssertionError Assert failed: (not-zero-length %)

Which is alot clearer.


Solution

  • This is discussed in the following clojure mailing list thread.

    Looking at the clojure.core source you can see the fn macro only passes in a boolean to the assert function, and does not include an optional parameter for passing an additional message argument in.

    So it looks like there is no way to do this cleanly yet.