大家好,
我正在与java-interop玩耍,并发现这个
(set! *warn-on-reflection* true)
(defonce ^:private ^ScheduledExecutorService executor (Executors/newScheduledThreadPool 1))
(defn set-timeout-primitive ^ScheduledFuture [^long ms ^Callable f]
(.schedule executor f ms TimeUnit/MILLISECONDS))
(defn set-timeout-object ^ScheduledFuture [^Long ms ^Callable f]
(.schedule executor f ms TimeUnit/MILLISECONDS))
(.get (set-timeout-primitive 2000 (fn [] (println :done) :done))) ; Reflection warning, ... - reference to field get on java.lang.Object can't be resolved.
(.get (set-timeout-object 2000 (fn [] (println :done) :done))) ; No warnings
这个反射警告的原因(感谢在这里解释)似乎是因为当编译器寻找匹配的接口时,它找到了这个
static public interface OLOO{Object invokePrim(Object arg0, long arg1, Object arg2);}
因此,返回值变成了Object
,而不是ScheduledFuture
。但使用^Long
类型提示而不是^long
时不会发生这种情况。
这可以改进吗?谢谢!