关于clojure:proxy和reify有什么区别?

What is the difference between proxy and reify?

代理和验证之间有什么区别?我有一些示例代码:

1
2
3
4
5
6
7
8
9
10
11
(.listFiles (java.io.File.".")
  (proxy
    [java.io.FileFilter] []
    (accept [f]
      (.isDirectory f))))

(.listFiles (java.io.File.".")
  (reify
    java.io.FileFilter
    (accept [this f]
      (.isDirectory f))))

结果是一样的,当使用代理或验证时,哪个更好?

更新:

我发现了一些东西:

  • 代理不需要this作为第一个参数。
  • 代理支持超类。
  • 代理支持参数。

来自Clojure.org的数据类型概述:

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.

来源:http://clojure.org/datatypes