In the simpler cases you can use `oneOf` with the subtypes first, but I
get your point. Plus, `andThen` will be much faster. ;)
On 21 Dec 2016, at 8:53, 'Rupert Smith' via Elm Discuss wrote:
On Wednesday, December 21, 2016 at 2:44:13 PM UTC, Brian Hicks wrote:
A previous version of the `andThen` documentation actually used
circles
and rectangles as the motivating example. It's since switched to
versioned
objects, which are maybe a little more realistic. Did you choose
circles
and rectangles as your example here for that reason, or are you
really
working with shapes? (I also wrote a blog post about `andThen`
<https://www.brianthicks.com/post/2016/06/17/how-does-json-decode-andthen-work/>
that explains that example, but beware the 0.17 syntax.)
If you don't have any reason to separate your `Circle` and
`Rectangle`
types, you might also consider defining them in `Shape` directly.
That way
`CircleAsShape` would become `Circle`.
No I am not really working with Circles and Rectangles. I am working
with a
data model and writing a code generator for it that automatically
writes
the Elm Encoder and Decoder for me. Fields can be optional, there can
be
recursion and there can be mutual recursion and their can be
sub-typing. So
I have to write these encoders and decoders fairly defensively to
allow for
the wide range of possible json they may encounter.
I did think about using Json.Decode.oneOf, but that depends on the
order of
testing the various subtypes, as one may have a set of fields that is
a
superset of another. I would have to do a topological sort over the
partial
super-set relationship amongst their fields. Even that might not work
as
with fields being optional, or two fields with the same name but
different
type, their might be no practical way to tell apart two different
sub-types
without a discriminator field.
I had not seen your helpful example at:
https://www.brianthicks.com/post/2016/06/17/how-does-json-decode-andthen-work/
only one on stack overflow that advocated using the oneOf approach.
--
You received this message because you are subscribed to a topic in the
Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/elm-discuss/0I25pg51rK8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
[email protected].
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Elm
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.