clojure-dev

Issues: https://clojure.atlassian.net/browse/CLJ | Guide: https://insideclojure.org/2015/05/01/contributing-clojure/
2019-05-23T12:33:44.264300Z

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

2019-05-23T12:34:25.265Z

When the input of the multi-method is a map, we often use namespaced keys inside that map to provision components

2019-05-23T12:34:38.265200Z

(which works well)

2019-05-23T12:35:03.265600Z

In other cases, we may have to call .addMethod on the multi directly

2019-05-23T12:35:36.266300Z

Since this involves interop and having to type hint to avoid reflection

2019-05-23T12:40:20.267900Z

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))

alexmiller 2019-05-23T12:43:30.268600Z

I don’t understand the use case - can you flesh it out more?

2019-05-23T13:07:45.269300Z

To try to clarify: without resorting to interop, the only way to add a method on a multimethod if with defmethod

2019-05-23T13:08:51.270300Z

which feels like a toplevel construct and expects an fntail

2019-05-23T13:09:14.270800Z

having a way to supply a dispatchvalue and function directly could be useful in some cases

2019-05-23T13:11:32.271500Z

one such case is if you want the method to capture bindings

2019-05-23T13:11:52.272100Z

(add-method some-multi :dispatch-val (bound-fn* f))

vlaaad 2019-05-23T13:11:57.272400Z

(defn add-method [multi-fn dispath-value f]
  (.addMethod ^clojure.lang.MultiFn multi-fn dispath-value f))
?

2019-05-23T13:12:08.272700Z

@vlaad

2019-05-23T13:12:23.273100Z

@vlaaad yes, this is what I was pointing to above

vlaaad 2019-05-23T13:12:39.273600Z

oh, you arguing it should be in core

2019-05-23T13:13:40.274100Z

not arguing at the moment, rather wondering 🙂

2019-05-23T13:14:01.274500Z

most of the time just talking about it shows why it's not a good idea in the first place

alexmiller 2019-05-23T13:18:38.274800Z

I don't understand why defmethod isn't fine?

alexmiller 2019-05-23T13:19:48.275600Z

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)

2019-05-23T13:23:13.276400Z

@alexmiller I agree that defmethod is fine for use in closures, and my issue is not with the name

2019-05-23T13:23:26.276800Z

rather, the lack of ability to be fed a function instead of an fntail

2019-05-23T13:23:38.277100Z

which makes some things a tad bit more cumbersome

alexmiller 2019-05-23T13:24:13.277800Z

can you give an example where that's cumbersome?

2019-05-23T13:24:20.278Z

restoring bindings

alexmiller 2019-05-23T13:24:26.278200Z

that's not an example

alexmiller 2019-05-23T13:24:38.278500Z

I mean code :)

2019-05-23T13:24:50.278900Z

ack, let me synthesize something

alexmiller 2019-05-23T13:25:26.279400Z

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

alexmiller 2019-05-23T13:25:47.279700Z

but I want to be compelled that it's needed :)

2019-05-23T13:29:09.280400Z

Switching that over to defmethod is completely feasible but requires a bit more gymnastics

alexmiller 2019-05-23T13:37:12.281500Z

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

2019-05-23T13:37:31.281700Z

I think the idea generalizes

2019-05-23T13:37:45.282100Z

our frontend people also had very similar hurdles with re-frame

2019-05-23T13:37:56.282300Z

iirc

2019-05-23T13:38:04.282500Z

will do

2019-05-23T13:38:16.282900Z

and thanks

dominicm 2019-05-23T14:05:09.284900Z

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.

dominicm 2019-05-23T14:05:44.285900Z

We have resorted to protocols when we need state, especially now that the metadata form is available.

2019-05-23T16:18:46.286500Z

hmm it seems the body is truncated and I cannot edit

2019-05-23T16:26:17.287Z

I think you may have opened an issue with jira cloud support

alexmiller 2019-05-23T16:26:27.287200Z

no, that's ours

alexmiller 2019-05-23T16:26:40.287600Z

I am still working on the portal for issue filing so you can be a guinea pig :)