3 ms·
On #5, the code isn't doing what the author may think: (let [#^int a 2 #^int b 3] (cons a b)) a and b are not hinted to be primitive ty
by twopoint718 17y ago
On #5, the code isn't doing what the author may think:
(let [#^int a 2
#^int b 3]
(cons a b))
a and b are not hinted to be primitive types. Type hinting is purely for objects. Use hinting to make it clear to the compiler what sort of objects you are passing into something (e.g. a constructor that takes a String). If there is confusion, clojure will do reflection.
The right thing to do in this case, is the second thing that the author mentions:
(let [a (int 2)
b (int 3)]
(cons a b))
a way to see that a and b are really primitive is:
(let [x (int 5)]
(.shortValue #^Integer x)) ; exception: "can't type hint primitive..."
There's a post on the mailing list that goes over this much better than I did (and where I drew the examples): http://groups.google.com/group/clojure/browse_thread/thread/b0554f7381b8db96/74bf85fabc12761a http://groups.google.com/group/clojure/browse_thread/thread/...
I understand that this is really beside the point, that the author finds type hinting syntax unattractive, but I just wanted to clear up confusion (if there was any).
EDIT: clarity