Closed noughtmare closed 3 years ago
I think writing manual instance is a way to go here.
I have written some code for enumeration types which use with DerivingVia
may look like
data Category
= WebDevelopment -- more consistent
| ObjectOriented
| Python
| ProgrammingLanguage -- more consistent
deriving (FromJSON, ToJSON) via NamedEnum '["Web development", "Object Oriented", ...]
In general, I'm open to embracing DerivingVia
instead of trying to make the single generics deriving
be capable of cater for all imaginable use cases.
However that doesn't have catch-all case. My experience suggests it is better to have strict enumeration,
and then maybe a custom wrapper (if you don't want to use Maybe
). e.g. Cabal
have KnownExtension
and Extension
(and few other similar pairs).
@phadej thanks for the suggestion. That DerivingVia
approach is also better because then the constructor names don't have to be changed.
Consider this data type and parsing code:
If you want to derive this parsing code automatically then you need to edit some of the fields and use customOptions:
But this doesn't catch the
Misc
case. I have tried wrapping theCategory
up in aMaybe
hoping that a failure to parse would result inNothing
, e.g.decode "\"ABC\"" :: Maybe (Maybe Category)
expectingJust Nothing
, but it returnsNothing
unfortunately. Is there a way to do this or could such functionality be added?This came up in the Monthly Hask Anything thread on reddit.