Skip to content

Support for algebraic sum types #4

Description

@liminalisht

First, let me say great work. I've really enjoyed working with servant-elm so far.

The only trouble I've run into so far with servant-elm occurs when the api for which I'm generating elm code involves algebraic sum types in some way.

E.g. Let's say my api looks like this:

type NameCompletionAPI = "nameCompletion" :> ReqBody '[JSON] NameCompletionRequest :> Get '[JSON] [Name]

And suppose the NameCompletionRequest type looks like this:

data NameCompletionRequest = NameCompletionRequest { nameSubstring :: Text , fromNationality :: Nationality , position :: Position } deriving (Generic)

and Position is a simple algebraic sum type:

data Position = Beginning | Middle | End deriving (Eq, Generic)

(Suppose too Nationality is just a new type wrapper around Text, and that we've defined all the ToJSON, FromJSON, and ElmType or ToElmType instances for these data types.)

Then on the main branch, when I go to generate this code, I'll get something like:
*** Exception: toElmTypeWithSources: Sum (Constructor "Beginning" Unit) (Sum (Constructor "Middle" Unit) (Constructor "End" Unit))

And on the devel branch, I'll get something like:

*** Exception: src/Elm/Encoder.hs:(13,1)-(58,28): Non-exhaustive patterns in function render

I was wondering if support for algebraic sum types was in the cards any time soon.

I guess this may be fundamentally more of an issue with elm-export. Let me know if it's best brought up somewhere else.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions