dimanche 6 février 2022

How to override a method on a reified type [duplicate]

I would like to override a method on a reified type, but preserve all other methods. What is the simplest way to do that? For example:

I have:

(deftype Adapter []
 some.namespace/SomeProtocol
 (type-name-get [this] (:Adapter))
 ...)
(def adapter-instance (Adapter.))

And I want to get:

(def modified-adapter-instance
 (some-magic-fn adapter-instance type-name-get (fn [this] :ModifiedAdapter)))
(.type-name-get adapter-instance) => :Adapter
(.type-name-get modified-adapter-instance) => :ModifiedAdapter

The best solution that I've found is here. Is uses delegating-proxy macro to create a proxy from the object. Then you can use update-proxy to override methods.





Aucun commentaire:

Enregistrer un commentaire