More readable posts with: pre and: post?

In clojure I use: pre like this

user=> (defn cannot-take-empty [x] {:pre [((complement empty?) x)]} 1) #'user/cannot-take-empty user=> (cannot-take-empty #{}) AssertionError Assert failed: ((complement empty?) x) user/cannot-take-empty (NO_SOURCE_FILE:186) 

This is great, but it does not explain the business reason why it makes no sense to go to an empty collection. (Either a collection with more than five elements, or a collection that has two keys but not the other, or something else that is the rule of the day.) This is potentially even more confusing for the user if the precondition uses a private function.

Is there a way to provide the user with more useful feedback, such as an error message when using: pre and: post?

+5
source share
1 answer

Apparently, the pre and post conditions are intended to be used in cases where reporting on proposals provides the developer with sufficient information, that is, it does not require explanation. If you want to give more explanation, then it is idiomatic to use assert .

But you can abuse the fact that the entire condition is always reported, for example. eg:

 {:pre [(do "It can't be empty because of..." (seq x))]} 

And he will report something like

AssertionError Assert failed: (do "Cannot be empty due to ..." (seq x)) ...

+4
source

All Articles