A blog about software development and other software related matters

Blog Archive

Thursday, July 23, 2009

Dont overflow yourself!

Clojure has many nice less highlighted features, one of these is overflow protection, this can be a true life saver:


user=> (unchecked-add 1 2); not safe but might be faster
3
user=> (unchecked-add (. Integer MAX_VALUE) 2); oops
-2147483647
user=> (+ (. Integer MAX_VALUE) 2) ; using safe arithmetic again
2147483649
user=> (+ (. Integer MAX_VALUE) 23)
2147483670
user=> (class (+ (. Integer MAX_VALUE) 23)); Clojure does the right thing for us
java.lang.Long


It does pack more than just concurrency up its sleeve.

No comments: