Welcome! Please see the About page for a little more info on how this works.

0 votes
in Multimethods by

Thanks to IMultiFn in CLJS types and records can be easily extended to behave as multimethods. For example:

`(defrecord MyMulti
[add-method remove-method dispatch-fn method-table]
(-add-method [_ dispatch-val f]

(add-method dispatch-val f))

(-remove-method [_ dispatch-val]

(remove-method dispatch-val))

(-methods [_] @method-table))

(def my-multi (->MyMulti ...))

(defmethod my-multi :foo

In Clojure however this isn't as easy because multimethods are not implemented using protocols. IS there ANY way to make a record or type to work with defmethod in CLJ?


1 Answer

+1 vote

No, this is not a feature I would expect to see in Clojure. I don't understand why it's even useful in ClojureScript?

In my case it's for a dispatch-based UI component library (similar to reagent). On init a record is instantiated that supports a few protocols, e.g. IDeref for state and IMultiFn to add UI component functions though defmethod.