servant-elm
servant-elm copied to clipboard
Support for algebraic sum types
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.
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.
Ok great I'll raise the issue there then. Thanks.
Is this still an issue?
elm-export still doesn't support sum types (but see https://github.com/krisajenkins/elm-export/pull/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.