Use andThen http://package.elm-lang.org/packages/elm-lang/core/5.0.0/Json-Decode#andThen
On Wednesday, 21 December 2016 13:38:14 UTC+1, Rupert Smith wrote: > > Struggling to figure this out, but it may be because I'm in an open plan > office and its just too noisy to concentrate... > > Suppose I have decoders for two records: > > type alias Circle = { radius : Float } > type alias Rectangle = { width: Float, height: Float } > > On the server side though, these are actually a class hierarchy, with > > Circle <: Shape > Rectangle <: Shape > > Where <: means 'subtype of'. > > When serializing over json, a discriminator field is added, so the json > looks like: > > { > @type : "Circle", > radius : ... > } > > { > @type : "Rectangle", > width : ... > } > > I have decoders for circles and rectangles already defined: > > circleDecoder : Decoder Circle > rectangleDecoder : Decoder Rectangle > > and am now trying to write the decoder for Shape: > > type Shape = > CircleAsShape Circle > | RectangleAsShape Rectangle > > shapeDecoder : Decoder Shape > shapeDecoder = > succeed (some function from '@type' to circle or rectangle decoder > mapped to shape decoder) > |: (field "@type" Decode.string) > > Feels about the right form for it. Exception this will return a Decoder > (Decoder Shape) instead of a Decoder Shape. > > Can someone explain how to do this? > -- 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 elm-discuss+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.