Fork me on GitHub
#malli
<
2021-04-02
>
Adam Helins07:04:02

I am wondering how Malli is performing in an inheritance-like scenario. Essentially, Malli allows keys to be :optional in :map . This is about having whole sets of keys being optional, in an all-or-none fashion, such as:

(def AnimalBase
     [:map ...])

(def Cat
     [:merge
      AnimalBase
      [:map ...]])

(def Dog
     [:merge
      AnimalBase
      [:map ...]])

(def Animal
     (m/schema [:multi {:dispatch :type-or-something} :cat Cat :dog Dog]))
Does it leverage the fact that the AnimalBase schema is mandatory in all dispatched schemas? Is there maybe an already existing better way of representing inheritance? Also assuming that being able to use generation is mandatory.

juhoteperi08:04:24

:merge with two map schemas creates a new map schema with keys from both schemas, so Cat and Dog don't refer to AnimalBase but are instead just map schemas with properties from the inputs

juhoteperi08:04:54

Or hmph, I might be confusing it with malli.util/merge... not sure if this works similarly

juhoteperi08:04:53

:merge should same as malli.util/merge

Adam Helins08:04:02

I believe malli.util/merge is indeed eager in that sense (creating a new map schema and forgetting about AnimalBase). But theoretically, since this example relies on data and :merge, that :multischema could deduce that all dispatched schemas are maps that could have a common set of keys. But it is just the most straightforward way of representing single inheritance IMO, maybe there is something better.

Adam Helins08:04:00

Sorry I realized my example was a bit misleading so I deleted it so it doesn't spam the channel. I'll work more on this and try to come back with a real-world one.

👌 3