Deriving multiple classes in standalone (or document this limitation)
Motivation
To the best of my knowledge (I could not find any information about it in the GHC Standalone Deriving documentation), it is not possible to derive multiple classes in a single standalone deriving declaration.
Repeating an identical deriving declaration only to change the derived instance seems like unnecessary boilerplate:
deriving instance Eq (MyType 'Foo)
deriving instance Show (MyType 'Foo)
deriving instance Read (MyType 'Foo)
But this situation can actually become bug-prone when used together with via
strategy, as it becomes easy to have discrepancies. Example using deriving-aeson library:
deriving via
( CustomJSON
'[ OmitNothingFields,
UnwrapUnaryRecords,
TagSingleConstructors,
FieldLabelModifier (StripPrefix "user", CamelToSnake)
]
)
instance
ToJSON (MyType 'Foo)
deriving via
( CustomJSON
'[ OmitNothingFields, -- Oh, we forgot to add 'UnwrapUnaryRecords' here!
TagSingleConstructors,
FieldLabelModifier (StripPrefix "user", CamelToSnake)
]
)
instance
FromJSON (MyType 'Foo)
Proposal
I propose GHC supports deriving several instances in a single standalone declaration. I don't have a strong opinion on the syntax (nor have I put a lot of effort in thinking about it, I have to admit), though I give an example of what it could look like below:
deriving instance (Eq, Show, Read) (MyType 'Foo)
deriving via
( CustomJSON
'[ OmitNothingFields,
UnwrapUnaryRecords,
TagSingleConstructors,
FieldLabelModifier (StripPrefix "user", CamelToSnake)
]
)
instance
(ToJSON, FromJSON) (MyType 'Foo)
Should this proposal get rejected, I think we should at least document this limitation.