How to decode tagged union types in Elm?
问题 If I have a certain tagged union type, like Shape here, how would I construct a JSON decoder for it in Elm? type alias Rectangle = { width : Int, height : Int } type alias Circle = { radius: Int } type Shape = ShapeRectangle Rectangle | ShapeCircle Circle 回答1: Given your JSON looks like { "radius" : 10 } or { "width" : 20, "height" : 15} Then this will do the trick import Json.Decode as Json exposing ((:=)) decodeShape : Json.Decoder Shape decodeShape = Json.oneOf [ decodeShapeRectangle ,