Java – type prompt functions in clojure
•
Java
I tried to solve the reflection warning in clojure, which seems to be due to the lack of type inference for the function return value of ordinary Java objects
Simple example code that demonstrates the problem:
(set! *warn-on-reflection* true) (defn foo [#^Integer x] (+ 3 x)) (.equals (foo 2) (foo 2)) => Reflection warning,NO_SOURCE_PATH:10 - call to equals can't be resolved. true
What is the best solution? Can this be done with type hints?
Solution
Both versions seem to work:
user> (defn foo [^Integer x] (+ 3 x)) #'user/foo user> (.equals (foo 2) (foo 2)) Reflection warning,NO_SOURCE_FILE:1 - call to equals can't be resolved. ;' true user> (.equals ^Integer (foo 2) ^Integer (foo 2)) true user> (defn ^Integer foo [^Integer x] (+ 3 x)) #'user/foo user> (.equals (foo 2) (foo 2)) true
Please note that clojure's type hint still has some limitations in version 1.2, so this may never be the same Also note that ^ ^ does not approve of the use of ^
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码