This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-12-23
Channels
- # adventofcode (135)
- # announcements (9)
- # babashka (27)
- # beginners (97)
- # bristol-clojurians (8)
- # calva (7)
- # chlorine-clover (1)
- # cider (3)
- # clara (16)
- # clj-kondo (9)
- # cljdoc (137)
- # clojars (4)
- # clojure (110)
- # clojure-europe (118)
- # clojure-taiwan (8)
- # clojure-uk (19)
- # clojurescript (30)
- # conjure (6)
- # cryogen (32)
- # datomic (11)
- # depstar (1)
- # duct (4)
- # emacs (6)
- # fulcro (73)
- # graalvm (9)
- # keechma (7)
- # leiningen (16)
- # luminus (1)
- # malli (35)
- # meander (3)
- # off-topic (45)
- # pathom (1)
- # pedestal (2)
- # re-frame (3)
- # reagent (31)
- # reitit (2)
- # reveal (17)
- # shadow-cljs (34)
- # tools-deps (11)
- # xtdb (14)
@ikitommi In other languages people usually define this as an ADT. Maybe :enum
can also be used for this?
:enum
is about values, :or
is about schemas:
[:or
[:map [:type [:= :work]] [:value string?]]
[:map [:type [:= :personal]] [:value string?]]]
fixed, thanks to @borkdudewhy did you have to write [:work [:map [:type [:= :work]] [:value string?]]]
with :multi
but [:map [:type :work] [:value string?]]
with :or
?
(`either` was deprecated in Plumatic Schema because of this, malli supports that as it’s usefull to describe the real world, despite being slow)
if you support :case
for a closed world of possibilities it could possibly be even faster?
but if the :type
effects the :value
, or any other parts of the schema, then :multi
is the way.
just a dispatch map from key -> schema. and the key is looked up using the :dispatch
function.
:dispatch
function is applied to value, which returns the :multi
key, which selects the schema
(m/validate
[:multi {:dispatch 'first}
[:sized [:tuple keyword? [:map [:size int?]]]]
[:human [:tuple keyword? [:map [:name string?] [:address [:map [:country keyword?]]]]]]]
[:human {:name "seppo", :address {:country :sweden}}])
; true
so, there is always a map-lookup, so not as fast as case
, but it would require code-generation, which would mean a macro.
could add a :conditional
, where the entry keys are functions, and the first one will match. Like :or
, but more explicit and short-circuits on first match:
[:conditional
[map? [:map [:x int?]]]
[int? [:int {:min 1, :max 2}]]]
malli.core is distilled into tons of helper functions to build new schemas, really easy to add things like this atm.
sorry, it does. I think the issue is that you can have multiple branches that would match, making the rest effectively unreachable.