Clojure: Using proxy and mutable fields

扶醉桌前 提交于 2019-12-31 03:06:23

问题


I'm using proxy in Clojure to extend a Java class. I need to set a field in the superclass, how can i do this? The code below doesn't work.

(proxy [BasicPlayer] []
  (open [url]
  (set! super/m_dataSource url)))

回答1:


From the documentation for proxy:

Note that while method fns can be provided to override protected methods, they have no other access to protected members, nor to super, as these capabilities cannot be proxied.

Sorry, but it sounds like you're out of luck. You can call protected superclass methods with proxy-super, but I think that's about it.

You might have better luck with gen-class. Something along the lines of:

(ns my.Player
  (:gen-class
   :extends BasicPlayer 
   :exposes {m_dataSource {:set -setDataSource}})

(defn -open [this url]
  (-setDataSource this url))


来源:https://stackoverflow.com/questions/23293814/clojure-using-proxy-and-mutable-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!