Fork me on GitHub
#re-frame
<
2019-12-17
>
donavan12:12:44

Do people ever create events with compound keys at all, is there a reason not to do it? E.G.:

(rf/reg-event-fx
  [:a-key dynamic-key :final-key]
  (fn [_ _]
    {}))

p-himik12:12:59

Not sure about dynamic-key, but I sometimes use compound keys that are just vectors of keywords. Usually it's just to avoid having to create another multimethod.

donavan12:12:21

Thanks! Yeah, that's what I'm trying avoid as well. dynamic-key works I was just wondering whether there was some reason to not rely on the compound key that I couldn't see.

p-himik12:12:37

Eh, not really a robust reason but some tools don't like them. At least, quite some time go re-frame-10x used to display them incorrectly, IIRC. That seems to be fixed now though. I think there was something with re-frisk as well. But I haven't seen anything like that recently, so yeah, probably nothing to worry about.

p-himik13:12:37

And by finding an issue in my code, I immediately realized a downside for this approach. You cannot have default values. For me, this is a significant advantage of multimethods. Of course, there's also dispatch values hierarchy but I don't use it that often.

donavan13:12:50

Ah, that's a good point. I don't have a default in this case but it makes me think I should just stick to multimethods to keep everything consistent.

steedman8721:12:24

how do compound keys work? I dont’ remember seeing those mentioned in the docs

p-himik07:12:12

Compound keys are no different from regular keyword keys. re-frame doesn't care what you use as a key, keywords are just a common practice.

bravilogy12:12:50

very interesting discussion. @, would you mind giving an example where compound keys are useful please?

p-himik12:12:25

@ Anywhere you'd use a multimethod but where using one is more clunky and not justified enough to not use a compound key.

bravilogy12:12:34

I don't think I've ever used a multi method when registering event handlers. I can't think of a use case đŸ˜„

p-himik12:12:39

Well, suppose you have a few panel types, and for each panel you need to fetch some data when a button is pressed somewhere on the panel. There are multiple ways to do it. One of them is to use a compound key like [:fetch-data panel-kind].