gpt4 book ai didi

clojure - Clojure 中的具体化是什么?

转载 作者:行者123 更新时间:2023-12-03 14:27:44 26 4
gpt4 key购买 nike

我无法理解 的用法物化 Clojure 中的函数。
它在clojure中有什么用?

你能提供例子吗?

最佳答案

reify宏允许创建一个匿名类扩展 java.lang.Object类和/或实现指定的接口(interface)/协议(protocol)。 API docs不要清楚地描述目的,而是提供该宏所做的技术细节。 Java interop documentation提供目的的简要说明:

As of Clojure 1.2, reify is also available for implementing interfaces.



更多信息可以在 datatypes documentatio 中找到。 n 在这里您可以找到非常详细的描述它的作用以及它与 proxy 的比较:

While deftype and defrecord define named types, reify defines both an anonymous type and creates an instance of that type. The use case is where you need a one-off implementation of one or more protocols or interfaces and would like to take advantage of the local context. In this respect it is use case similar to proxy, or anonymous inner classes in Java.

The method bodies of reify are lexical closures, and can refer to the surrounding local scope. reify differs from proxy in that:

Only protocols or interfaces are supported, no concrete superclass. The method bodies are true methods of the resulting class, not external fns. Invocation of methods on the instance is direct, not using map lookup. No support for dynamic swapping of methods in the method map. The result is better performance than proxy, both in construction and invocation. reify is preferable to proxy in all cases where its constraints are not prohibitive.

关于clojure - Clojure 中的具体化是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058268/

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