4 ms·
The overall conclusion is correct, most error messages happen by accident, but a lot the examples look like expected behavior to me. Maybe because Ive been usin
by lbj 8y ago
The overall conclusion is correct, most error messages happen by accident, but a lot the examples look like expected behavior to me. Maybe because Ive been using Clojure for too long, or maybe because its the expected trade-off when using a dynamically typed language. You get more fluidity at the cost of type checks - The way to mitigate this downside is adding checks of your own. Clojure has supported this since way back when using pre/post conditions.
Example:
core> (defn mydiv [n d] {:pre [(pos? d)]} (/ n d))
#'mydiv
core> (mydiv 10 2)
5
core> (mydiv 10 0)
AssertionError Assert failed: (pos? d) core/mydiv
As opposed to the standard
core> (/ 10 0)
ArithmeticException Divide by zero
clojure.lang.Numbers.divide (Numbers.java:158)
Chasing down Numbers.java will take time, but with a pre-condition you get the exact name of the function and the test which failed.
- puredanger 8y agoBefore going down this path, it's important to understand the substantial performance implications.
- lbj 8y agoIndeed
- jonstaab 8y agoChecks are turned off by default. Are there performance implications even when they're turned off? Reference: https://clojure.org/guides/spec https://clojure.org/guides/spec
- ambulancechaser 8y agospec is wholly independent of the pre/post conditions.
- aperiodic 8y agoIn this respect, they share the characteristic of being easy to wholly disable (i.e. prevent them from being compiled at all). With pre & post conditions if the var `clojure.core/assert` is bound to false, then they're elided. The usual pattern is to have them on for testing, and disable when compiling for production.
- puredanger 8y agoThe math functions are statically inlined for performance. Wrapping them in another level of function/var will have a significant impact on performance even if assertions for preconditions are turned off. Spec assertions (not pre/post checks) can actually be compiled completely out of production code so those can really be nothing (but not if you're adding them in wrapper layers).