sci
sci copied to clipboard
Allow combinations of classes and protocols in defrecord
We can maybe apply the same strategy as #541 for defrecord.
/cc @GreshamDanielStephens
We should at the very least support this:
(defrecord Foo [] ... some protocols ... Object (toString [this] (str ":foo")))
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)
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.