While we are talking about assert: I recently wanted for assert to  
return the value of the expression, so I could embed asserts inside a  
Fact test and get detailed reporting about what failed.

I checked a few other functional languages and none of their asserts  
return a value either. This surprised me.

Should assert return its value? If not, what should I name the  
function that works just like assert but returns its value?

Stuart

> At the moment assert macro accepts only a single argument - a test. If
> the test fails (is false), an exception is thrown. But the only
> information available (until we have better introspection tools) is
> the test _expression_. This causes problems when working on many items
> that should satisfy an assertion, but the assertion expression is the
> same for all of them (like, when iterating through a sequence) - it is
> impossible to see for which items exactly the assertion fails.
>
> For this reason I think assert macro should accept another parameter -
> something to construct a meaningful assertion failure reason. A
> simplistic approach would be to just pass a string that has been
> constructed using str function. But this would involve too much
> unnecessary computation/consing since assertions are usually expected
> not to fail (in which case the failure message is not needed and
> should not be constructed). So my proposed (below) accepts a second
> parameter, which should be a form that results in a string. It could
> also accept variable number of parameters and implicitly call str on
> them, but that I think is a bad style.
>
> Comments welcome. And I hope some variation of it makes it to the
> clojure.core.
>
> (defmacro assert
>  "Evaluates expr and throws an exception if it does not evaluate to
> logical true. message, if present, should be a an expression that
> evaluates to
> string describing the assertion failure."
>  ([x]
>     `(when-not ~x
>        (throw (new Exception (str "Assert failed: " (pr-str '~x))))))
>  ([x message]
>     `(let [message# (fn [] ~message)]
>        (when-not ~x
>          (throw (new Exception (str "Assert failed:
> " (message#))))))))
>
> --
> Jānis Džeriņš
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to