sci icon indicating copy to clipboard operation
sci copied to clipboard

Allow combinations of classes and protocols in defrecord

Open borkdude opened this issue 4 years ago • 3 comments

We can maybe apply the same strategy as #541 for defrecord.

/cc @GreshamDanielStephens

borkdude avatar Mar 02 '21 20:03 borkdude

We should at the very least support this:

(defrecord Foo [] ... some protocols ... Object (toString [this] (str ":foo")))

borkdude avatar Mar 19 '21 09:03 borkdude

Perhaps DynamicProxy can help us out here.

(ns dynproxy
  (:import [java.lang.reflect InvocationHandler Proxy]))

(set! *warn-on-reflection* true)

(defn handler []
  (reify InvocationHandler
    (invoke [this proxy method args]
      ;; (prn args)
      42)))

(defn new-proxy []
  (Proxy/newProxyInstance (.getClassLoader clojure.lang.RT)
                          (into-array Class [java.util.Map])
                          (handler)))

(prn (class (new-proxy)))
(.put ^java.util.Map (new-proxy) 1 2)

borkdude avatar Jul 18 '21 09:07 borkdude

Trying the above in the dynamicproxybranch

 $ ./sci '(def x (new-proxy [java.util.Map])) (prn x) (let [^java.util.Map x ^java.util.Map x] (.put x 1 2))'

but GraalVM gives Generating proxy classes at runtime is not supported. Proxy classes need to be defined at image build time by specifying the list of interfaces that they implement. I have the feeling this might change in 21.3 so might be a way in the future.

borkdude avatar Jul 18 '21 10:07 borkdude