Precedence of clojure protocols in another namespace
在使用
当我处理完各个数据结构后,应该可以做一个
但是,在
我可以在名称空间中添加
因此,我最终不得不分叉
有没有办法防止协议中的
如果没有,是否可以在另一个名称空间中撤回某个类型的协议?关于将协议编译到Java接口中的方式,甚至可以实现它?
它应该工作
您确定不能为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | user=> (ns ns1) ;;=> nil ns1=> (defprotocol IPrintable (prnt [this])) ;;=> IPrintable ns1=> (extend-protocol IPrintable clojure.lang.Associative (prnt [this] (str"clojure.lang.Associative" this))) ;;=> nil ns1=> (ns ns2) ;;=> nil ns2=> (ns1/prnt {:a 1}) ;;=>"clojure.lang.Associative {:a 1}" ns2=> (extend-protocol ns1/IPrintable clojure.lang.Associative (prnt [this] (str"My custom impl for clojure.lang.Associative" this))) ;;=> nil ns2=> (ns1/prnt {:a 1}) ;;=>"My custom impl for clojure.lang.Associative {:a 1}" |
它也适用于示例项目。
如果
什么时候不行
当对象直接实现接口或协议时(例如
1 2 3 4 5 6 7 8 9 | ns2=> (defrecord SomeData [] ns1/IPrintable (prnt [this]"I'm SomeData")) ;;=> ns2.SomeData ns2=> (ns1/prnt (SomeData.)) ;;=>"I'm SomeData" ns2=> (extend-protocol ns1/IPrintable SomeData (prnt [this]"Custom impl" this)) ;;=> IllegalArgumentException class ns2.SomeData already directly implements interface ns1.IPrintable for protocol:#'ns1/IPrintable clojure.core/extend (core_deftype.clj:775) |