Share your thoughts in the 2024 State of Clojure Survey!

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]
IMultiFn
(-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
[x]
...)
`

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?

Thanks!

1 Answer

+1 vote
by

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

by
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.
...