gpt4 book ai didi

clojure - 可以记住 Clojure 中的 Java 反射吗?

转载 作者:行者123 更新时间:2023-12-04 23:10:44 26 4
gpt4 key购买 nike

类型提示可以极大地改善反射发生多次的执行时间。我对类型提示的理解是它只允许编译器缓存反射查找。缓存可以动态发生吗?或者有什么原因这会很糟糕/不可能?

最佳答案

来自 Programming Clojure :

These warnings indicate that Clojure has no way to know the type of c. You can provide a type hint to fix this, using the metadata syntax ^Class:


(defn describe-class [#^Class c]
{:name (.getName c)
:final (java.lang.reflect.Modifier/isFinal (.getModifiers c))})

With the type hint in place, the reflection warnings will disappear. The compiled Clojure code will be exactly the same as compiled Java code. Further, attempts to call describe-class with something other than a Class will fail with a ClassCastException.



所以总而言之,反射转换不仅仅是缓存,它被消除了。

关于clojure - 可以记住 Clojure 中的 Java 反射吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1485341/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com