Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to decode union types in Elm
import Html exposing (text)
import Json.Decode exposing (..)
type Status
= Done
| Open
| Failed
type alias Stuff =
{ f: Status }
statusDecoder =
statusHelper s =
case s of
"Done" -> Done
"Open" -> Open
_ -> Failed
map statusHelper string
stuffDecoder =
map Stuff
(field "f" statusDecoder)
main =
"{\"f\": \"Done\"}"
|> decodeString stuffDecoder
|> toString
|> text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.