IMO this is good programming practice. If there exists a reasonable contract to check your argument/return value, then use it.
You mentioned protocols; if an argument extends a protocol, use {:pre [(satisfies? P a)]} to check your argument. The payoff is localised error messages. Clojure is type safe so we're not going to crash our VM without such validation. Other programmers can also more easily reason about your code. :pre/:post are fairly blunt tools. See contracts libraries like core.contracts and Schema for better experiences. Thanks, Ambrose On Wed, Jul 23, 2014 at 6:59 PM, Wesley Hall <wesley.h...@gmail.com> wrote: > Hello folks, > > My question is, "Is it 'idiomatic' to perform type checking of function > arguments in :pre conditions"? > > Coming to clojure from 15 or so years of Java development has been > something of a revelation to me, but I am worried that I am still > struggling a bit with some old habits, one of which is to type check my > function arguments. I cannot get over this feeling of, "OMG!! What if > somebody passes a string to this function that requires a function > argument!! The world will surely end!". > > I am finding myself developing a bit of a habit of introducing type checks > in :pre conditions, but I am not sure whether the more 'clojure' thing to > do is to allow the exception to occur further down the call stack when the > reference is actually used in an inappropriate way (I attempt to call the > string as if it were a function). > > I have found a few cases where the error 'invites me' to implement a > protocol to 'teach' the system how to handle the particular type, which > tends to suggest to me that it might be better not to type check in a pre > condition, since at some later point I could decide to make the argument > type valid by defining a protocol implementation to handle the type > successfully. > > I am also aware of the existence of core.typed, which is perhaps something > I should look into in more detail. > > Do people have thoughts on this? How do you handle this in your production > clojure code? > > Thanks > > Wes > > -- > 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 > Note that posts from new members are moderated - please be patient with > your first post. > 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 > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- 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 Note that posts from new members are moderated - please be patient with your first post. 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.