Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between proxy and reify?

Tags:

clojure

What is the difference between proxy and reify? I have some example code:

(.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))))

the result is same, when use proxy or reify, what is better?

Update:

I found something:

  • proxy no need the this as first parameter.
  • proxy support superclass.
  • proxy support arguments.
like image 203
number23_cn Avatar asked Jun 13 '12 08:06

number23_cn


1 Answers

From Clojure.org's overview of data types:

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.

Source: http://clojure.org/datatypes

like image 185
Viezevingertjes Avatar answered Oct 17 '22 08:10

Viezevingertjes