| Is the behavior of `+` platform-specific for Clojure and CLJS? | Shantanu Kumar | 31/08/12 00:12 | Hello,
On the Clojure REPL I notice this: user=> (+ 1 2) 3 user=> (+ 1 "er") ClassCastException java.lang.String cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:126) user=> (+ "we" "er") ClassCastException java.lang.String cannot be cast to java.lang.Number clojure.lang.Numbers.add (Numbers.java:126) user=> (1 "we" 1) ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/eval952 (NO_SOURCE_FILE:1) However, on the ClojureScript REPL I notice the following: ClojureScript:cljs.user> (+ 1 2) 3 ClojureScript:cljs.user> (+ 1 "er") "1er" ClojureScript:cljs.user> (+ "we" "er") "weer" ClojureScript:cljs.user> (- "we" 1) NaN This difference in behavior can be explained by platform-specific semantics, but I want to know whether this is by design. Shantanu |
| Re: Is the behavior of `+` platform-specific for Clojure and CLJS? | David Nolen | 31/08/12 05:24 | No one has offered a way to prevent that without slowing things down. -- |
| Re: Is the behavior of `+` platform-specific for Clojure and CLJS? | Rich Hickey | 31/08/12 06:23 | To expound:
(+ "we" "er") is a user error. We rely on the runtime type system of the host to efficiently detect type errors. Unfortunately, the JS runtime does not consider that an error, thus it is not generating an exception. That is much different from there being semantics for (+ "we" "er") - there aren't. Nor is it guaranteed to continue to produce "weer". Nowhere does Clojure promise that all user errors will become exceptions, and it is a constant balancing act to add more checks without compromising the performance of correct programs. If and when we have a debug mode of runtime these kind of checks would be possible there. Rich > > To post to this group, send email to clo...@googlegroups.com<javascript:;> > > Note that posts from new members are moderated - please be patient with> > clojure+u...@googlegroups.com <javascript:;> |
| Re: Is the behavior of `+` platform-specific for Clojure and CLJS? | Shantanu Kumar | 31/08/12 10:29 | Thanks so much for the insight, Rich.
Shantanu |