In situations where you are using a dependency injection mechanism like component, it can sometimes be cumbersome to take advantage of multi fns, since you have to figure out a way to hand dependencies over to the method being installed
When the input of the multi-method is a map, we often use namespaced keys inside that map to provision components
(which works well)
In other cases, we may have to call .addMethod
on the multi directly
Since this involves interop and having to type hint to avoid reflection
I was wondering whether it could be considered to add an add-method
function which would essentially be (eliding a few things):
(defn add-method [^clojure.lang.MultiFn multifn dispatchval f] (.addMethod multifn dispatchval f))
I don’t understand the use case - can you flesh it out more?
To try to clarify: without resorting to interop, the only way to add a method on a multimethod if with defmethod
which feels like a toplevel construct and expects an fntail
having a way to supply a dispatchvalue and function directly could be useful in some cases
one such case is if you want the method to capture bindings
(add-method some-multi :dispatch-val (bound-fn* f))
(defn add-method [multi-fn dispath-value f]
(.addMethod ^clojure.lang.MultiFn multi-fn dispath-value f))
?@vlaad
@vlaaad yes, this is what I was pointing to above
oh, you arguing it should be in core
not arguing at the moment, rather wondering 🙂
most of the time just talking about it shows why it's not a good idea in the first place
I don't understand why defmethod isn't fine?
defmethod could just as easily have been called add-method (I think the "def" is really there to signal stateful var-level change, but you're getting that regardless)
@alexmiller I agree that defmethod is fine for use in closures, and my issue is not with the name
rather, the lack of ability to be fed a function instead of an fntail
which makes some things a tad bit more cumbersome
can you give an example where that's cumbersome?
restoring bindings
that's not an example
I mean code :)
ack, let me synthesize something
conceptually, I think it would be perfectly fine to have an add-method in core, that defmethod used, but which could be used directly if needed
but I want to be compelled that it's needed :)
Switching that over to defmethod is completely feasible but requires a bit more gymnastics
would be fine to ticket this. having a more complete example would be helpful particularly as this seems pretty specific to the lifecycle patterns imposed by component
I think the idea generalizes
our frontend people also had very similar hurdles with re-frame
iirc
will do
and thanks
We've discussed this internally, I don't think this is a good idea. Multimethods are global, and component is local. You can run multiple of the same system in parallel. This would prevent you from running tests at the same time as your REPL.
We have resorted to protocols when we need state, especially now that the metadata form is available.
@alexmiller I created https://clojure.atlassian.net/servicedesk/customer/portal/1/SUPPORT-5
hmm it seems the body is truncated and I cannot edit
I think you may have opened an issue with jira cloud support
no, that's ours
I am still working on the portal for issue filing so you can be a guinea pig :)