Welcome! Please see the About page for a little more info on how this works.

+3 votes
in Clojure by

It doesn't seem like that much of a shortcut, and it cuts me off from useful features like adding a message to assertions.

3 Answers

+4 votes
by

If you prefer assert and its messages, then it seems never.

by
True, the pre/post conditions feel like they never got expanded upon. Like they are missing a feature for error message.
by
I think your answer is the correct practical use one.

For the most part, nobody is using this metadata. It seems that spec is the preferred way to do the capabilities that would have come with :pre/:post and were never capitalized on.
+4 votes
by

Michael Fogus (author of The Joy of Clojure):

One advantage that :pre and :post have over assert is that the former allows the “assertions” to potentially come from a different source than the body of the function. [...] :pre and :post also make it relatively easy to do some fancy automatic test generate and as a bonus provide a form of documentation for the function. Chris Houser added another interesting point that I was not aware of, “using assert instead of :pre is usually pretty straightforward, if you had to do it. Using assert instead of :post is a pain.” Sounds reasonable. Along that point, Chas Emerick’s assessment that :pre and :post cover 99.5% of assert uses, plus provide simple, and consistent hooks.

Source: fogus: Clojure’s :pre and :post, comment number 14.

by
I spent a while trying to figure out how to do some of the things mentioned.

:pre/:post are available as metadata on the :arglists of the metadata of the var. So you could read these and use it to test generate. This makes sense, and I see how it could happen (especially with spec).

Changing that metadata doesn't change the assertions! So I don't see how you can have them come from a source which isn't the body.
+1 vote
by

You should also consider function specs: https://clojure.org/guides/spec#_specing_functions

...