This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-02-29
Channels
- # announcements (1)
- # babashka (5)
- # beginners (64)
- # cider (9)
- # circleci (11)
- # clojure (80)
- # clojure-uk (25)
- # clojured (6)
- # clojurescript (35)
- # css (1)
- # cursive (3)
- # datomic (6)
- # fulcro (10)
- # graphql (3)
- # kaocha (2)
- # leiningen (3)
- # lumo (3)
- # malli (10)
- # meander (24)
- # off-topic (17)
- # re-frame (10)
- # reagent (2)
- # ring (1)
- # shadow-cljs (27)
- # spacemacs (3)
- # tree-sitter (3)
I’ve been trying something different and bundling functionality together using protocols+records. Now I have a situation that I need to take an instance of a record and say that something happened, now this thing can do these other things as well. And what I would rather not do is just create a new concrete type… I’ve been thinking that maybe I should fall back to multimethods or perhaps I should be using multimethods in the first place… Not thought about this a lot, yet, so just wondering if this is well trodden ground.
Use multimethods for logic that differ depending on the parameters to the function, meaning instead of nested if
, cond
, and so on.
I'd say avoid using protocols for business entities (those that are typically saved in a DB). Instead use protocols for system components so you can have different implementations, so for things that have behavior, not data (though you can have processing state).
That’s fair, in general I do that, in this specific case I’m trying to work out how to organise a fairly complex simulation and trying to work out how to: 1) Keep the complexity down 2) Ensure that only entities that have certain qualities (components) can do things 3) Keep things reasonably fast
So I started with protocols because of 3, and I’m now realising that because my entities in my sim can gain and lose components this leads to types being a poor fit as I can’t just union them, which leads to two solutions: 1) Model types one level down, so they’re just a set or vector inside the entity 2) Keep things to just being maps. Maybe namespaced maps…
I see, protocol polymorphism is tied to the class/type of an object, but your use case requires polymorphism at instance level, so I'd avoid protocols and use some sort of :qualities
vector entry on which multimethods can do polymorphism.
You could still use protocols for class level polymorphism and add multimethods on qualities, if there is a nice split between both
some inspiration: https://github.com/markmandel/brute/blob/a4a374231393da0a3c652c7af40de7c676101443/src/brute/entity.cljc#L38
doesn't =
do the trick ?
you mean you want a false
result if the ordering is different?
ha, I've never thought of sorted-map
like that
is (= (seq a) (seq b))
what you want then?