Is the behavior of `+` platform-specific for Clojure and CLJS?

168 views
Skip to first unread message

Shantanu Kumar

unread,
Aug 31, 2012, 3:12:44 AM8/31/12
to Clojure
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

David Nolen

unread,
Aug 31, 2012, 8:24:24 AM8/31/12
to clo...@googlegroups.com
No one has offered a way to prevent that without slowing things down.
--
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

Rich Hickey

unread,
Aug 31, 2012, 9:23:52 AM8/31/12
to Clojure
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
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+u...@googlegroups.com <javascript:;>

Shantanu Kumar

unread,
Aug 31, 2012, 1:29:20 PM8/31/12
to Clojure
Thanks so much for the insight, Rich.

Shantanu
Reply all
Reply to author
Forward
0 new messages