On Dec 13, 5:24 am, ajay gopalakrishnan <ajgop...@gmail.com> wrote:
> It tried the following in REPL and got no error. Personally, I feel that I
> should get an error because calling square on strings is wrong in both
> cases.
>
> Is there a way out of this in Clojure?

I hope you don't mind me bumping this old thread, but only yesterday I
stumbled upon the "assert-args" macro in the master branch's core.clj
that does something similar to what you want. Example usage:

(defmacro when-let
  "bindings => binding-form test

  When test is true, evaluates body with binding-form bound to the
value of test"
  [bindings & body]
  (assert-args when-let
     (vector? bindings) "a vector for its binding"
     (= 2 (count bindings)) "exactly 2 forms in binding vector")
  ... snip body ...

I haven't tested it, but it will probably be able throw a compile time
exception if used in a macro definition (with it's checking ability
being limited by what is known at compile/macro expansion time), and a
runtime exception if used in a function definition. So if you make use
of assert-args in the function that will later call square, the error
can at least be caught at runtime and generate a useful exception
message, regardless of whether (map square ...) will be evaluated or
not.

assert-args is a private var though, so a more officially idiomatic
way to check function arguments is probably to use the new pre/post
conditions slated for 1.1.

(Only when I had finished writing this post did I realize that you
really want static type inferrence, which assert-args doesn't provide,
but hopefully this information will be useful anyway :-)

-- 
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

Reply via email to