Add (or document if already exist) ability to derive custom typeclasses via source plugins
Problem
Suppose, I have some custom typeclass Foo
defined in some library foo
:
class Foo a where
... some methods ...
I would like to be able to derive instances of this typeclass for any possible data type using deriving
clause just like GHC already does for typeclasses Eq
, Ord
, Show
, Read
, Enum
, etc.:
data Bar = Bar | Baz
deriving (Eq, Ord, Foo)
There're already two possible ways to derive instances of custom typeclasses:
-
anyclass
deriving strategy (usually involvesGeneric
) -
-XTemplateHaskell
solution.
But I would like to have source-plugin-based solution for this problem so I can just add -fplugin=Foo.Plugin
and enjoy deriving capabilities.
Advantage over existing approaches
Solution with -XTemplateHaskell
is not that pleasant to write and easy to maintain (you need to use libraries like http://hackage.haskell.org/package/th-abstraction to support multiple GHC versions),involves scoping restriction and is syntactically uglier. Compare:
{-# LANGUAGE TemplateHaskell #-}
data Bar = Bar | Baz
deriving (Eq, Ord)
deriveFoo ''Bar
Solution with something like Generic
introduces performance overhead (required for to/from generic representation conversion). This might not be significant for something like parsing CLI arguments but it's more important if you want to have efficient binary serialisation.
Also, it's known that deriving typeclasses is a relatively slow compilation process (https://github.com/tfausak/tfausak.github.io/issues/127) so there's slight chance that deriving typeclass manually can be slightly faster than deriving Generic + MyClass
. Especially when maintainers of plugins can experiment with some caching strategies for deriving typeclasses.
Trac metadata
Trac field | Value |
---|---|
Version | 8.6.1-beta1 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |