Permalink
Browse files

proxy & reify

  • Loading branch information...
Jay Fields
Jay Fields committed Dec 24, 2011
1 parent ea5a526 commit 783eb6f61cb37bf617e645e1ecea7156bc221754
Showing with 15 additions and 8 deletions.
  1. +14 −7 src/clojure/interop/core.clj
  2. +1 −1 src/java/interop/Example.java
@@ -1,11 +1,18 @@
(ns interop.core)
(defn print-string [arg]
;;; pass a Clojure vector where Java expects a java.util.Collection
(println (java.util.HashSet. ["1" "2"]))
(defn proxy-coll []
(proxy [java.util.Collection] []
(add [o]
(println o)
true)))
;;; pass a Clojure map where Java expects a java.util.Map
(println (java.util.LinkedHashMap. {1 "1" 2 "2"}))
(defn reify-coll []
(reify java.util.Collection
(add [this o]
(println o)
(println this)
true)))
;;; pass a Clojure function where Java expects a Runnable
(println (Thread. (fn [] (println "clojure fns are runnables (and callables)")))))
(defn main []
(.add (proxy-coll) "this string is printed on proxied.add")
(.add (reify-coll) "this string is printed on reified.add"))
@@ -5,6 +5,6 @@
public class Example {
public static void main(String[] args) throws Exception {
RT.loadResourceScript("interop/core.clj");
RT.var("interop.core", "print-string").invoke("hello world");
RT.var("interop.core", "main").invoke();
}
}

0 comments on commit 783eb6f

Please sign in to comment.