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?
(defn square [n] (* n n))
(if (= 0 0) (println "hello") (map square ["a" "b"]))
The following gives error (because it gets evaluated):
(defn square [n] (* n n))
(if (= 0 1) (println "hello") (map square ["a" "b"]))
Thanks,
Ajay
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
Oh ... I know all that. What I wanted to know is that is there any way to FORCE compile time checking by providing some flag or the other to Clojure compiler. If not, I guess a good set of test cases is the only way to fix it. (Good test cases are always recommended, it's just that in this case it becomes unavoidable)
OTOH, when Rich gave a talk at MIT recently he mentioned he wasn't terribly interested in type systems for Clojure apart from (potentially) a simple binary type system to distinguish I/O-side-effecty things from non-I/O-side-effecty things. He also mentioned something extremely interesting about wanting to make Clojure amenable to analysis by external static correctness checking tools, rather than build types and type-checking into the language itself.
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 :-)