RE: clojure.spec - Using :pre conditions (or not)?

2017-06-08 Thread David Goldfarb
@googlegroups.com [mailto:clojure@googlegroups.com] On Behalf Of Alex Miller Sent: Wednesday, June 7, 2017 5:46 PM To: Clojure <clojure@googlegroups.com> Subject: Re: clojure.spec - Using :pre conditions (or not)? Preconditions are already assertions, so it makes more sense to use s/assert in you

Re: clojure.spec - Using :pre conditions (or not)?

2017-06-07 Thread Alex Miller
Preconditions are already assertions, so it makes more sense to use s/assert in your code body than in a precondition. On Wednesday, June 7, 2017 at 8:12:22 AM UTC-5, David Goldfarb wrote: > > One big downside of using s/assert in a precondition: It does not work > with (s/nilable ...) specs,

Re: clojure.spec - Using :pre conditions (or not)?

2017-06-07 Thread David Goldfarb
One big downside of using s/assert in a precondition: It does not work with (s/nilable ...) specs, since s/assert returns valid values. I fell into this trap for a moment of head-scratching just now. On Wednesday, September 14, 2016 at 4:59:09 PM UTC+3, Alex Miller wrote: > > Another option

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-16 Thread joakim . tengstrand
I agree with you that an IllegalArgumentException is preferable to AssertionError. The reason that I used AssertionError was that I wanted to keep the same behaviour as when using s/valid? (it throws an AssertionError). Maybe only s/assert should throw AssertionError and s/valid? should throw

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-15 Thread Mamun
When function is throwing exception because of argument. I would prefer to throw IllegalArgumentException not AssertionError. (defn check [type data] (if (sp/valid? type data) true (throw (IllegalArgumentException. (sp/explain type data) Br, Mamun On Friday, September 16,

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-15 Thread joakim . tengstrand
I came up with this solution: (ns spec-test.core (:require [clojure.spec :as s])) (s/def :user/name string?) (s/def :common/user (s/keys :req [:user/name])) ;; with this little helper function... (defn check [type data] (if (s/valid? type data) true (throw (AssertionError.

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-15 Thread joakim . tengstrand
Thanks for the tip! On Thursday, September 15, 2016 at 3:11:32 PM UTC+2, Shantanu Kumar wrote: > > Hi Joakim, > > You might be interested in Paul Stadig's library > https://github.com/pjstadig/assertions that leverages Java's `-ea` > (enable-assertions, which you may want to keep enabled in

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-15 Thread Shantanu Kumar
Hi Joakim, You might be interested in Paul Stadig's library https://github.com/pjstadig/assertions that leverages Java's `-ea` (enable-assertions, which you may want to keep enabled in dev) command-line flag. If you have a bunch of things together to assert, you may want to use the

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-15 Thread joakim . tengstrand
Ok, thanks! In the Java world, the assertions is also something that need to be turn on explicitly. In that sence, they are kind of not mandatory to be executed (or at least signals that to the reader of the code). I would be happier if you guys could add another method, that I can use in my

Re: clojure.spec - Using :pre conditions (or not)?

2016-09-14 Thread Alex Miller
Another option that has been added since the guide was written is s/assert which seems closer to what you're suggesting. (defn name [user] {:pre [(s/assert :common/user user)]} (-> user :user/name)) ;; need to enable assertion checking - this can also be enabled globally with system