Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for algebraic sum types #4

Open
liminalisht opened this issue Apr 5, 2016 · 4 comments
Open

Support for algebraic sum types #4

liminalisht opened this issue Apr 5, 2016 · 4 comments

Comments

@liminalisht
Copy link

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.

@mattjbray
Copy link
Collaborator

Thanks!

Yes, servant-elm is really just a thin wrapper around elm-export, which does not yet support generating Elm encoders/decoders for sum types. As far as I know there's no technical reason for this, it just hasn't been done yet. So the proper place to raise this is over at krisajenkins/elm-export.

As an added complication, servant-elm currently uses a forked version of elm-export that adds a couple of features. I hope that they will be accepted upstream, but in the meantime let's leave this issue open to remind me to rebase the fork when sum type encoders/decoders land.

@liminalisht
Copy link
Author

Ok great I'll raise the issue there then. Thanks.

@fosskers
Copy link

Is this still an issue?

@mattjbray
Copy link
Collaborator

elm-export still doesn't support sum types (but see krisajenkins/elm-export#34 for some promising progress).

On the other hand, servant-elm no longer uses a fork of elm-export, so perhaps we can close this.

ollef pushed a commit to folq/servant-elm that referenced this issue Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants