[generics-rep] Newtypes for deriving via
With deriving via and some newtypes we could simulate defaults methods and alleviate a bit the implementation of typeclasses with generic defaults.
For instance implementing Show, Enum and Bounded:
module Example where
import Data.Enum (class Enum)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Bounded (genericBottom, genericTop)
import Data.Generic.Rep.Enum (genericSucc, genericPred)
import Data.Generic.Rep.Show (genericShow)
data ABC = A | B | C
derive instance eqABC :: Eq ABC
derive instance ordABC :: Ord ABC
derive instance genericABC :: Generic ABC _
instance showABC :: Show ABC where
show = genericShow
instance boundedABC :: Bounded ABC where
top = genericTop
bottom = genericBottom
instance enumABC :: Enum ABC where
succ = genericSucc
pred = genericPred
Could be a bit more compact:
module Example where
import Data.Enum (class Enum)
import Data.Generic.Rep (class Generic)
-import Data.Generic.Rep.Bounded (genericBottom, genericTop)
+import Data.Generic.Rep.Bounded (GenericBounded)
-import Data.Generic.Rep.Enum (genericSucc, genericPred)
+import Data.Generic.Rep.Enum (GenericEnum)
-import Data.Generic.Rep.Show (genericShow)
+import Data.Generic.Rep.Show (GenericShow)
data ABC = A | B | C
derive instance eqABC :: Eq ABC
derive instance ordABC :: Ord ABC
derive instance genericABC :: Generic ABC _
+derive via (GenericShow ABC) instance showABC :: Show ABC
-instance showABC :: Show ABC where
- show = genericShow
+derive via (GenericBounded ABC) instance boundedABC :: Bounded ABC
-instance boundedABC :: Bounded ABC where
- top = genericTop
- bottom = genericBottom
+derive via (GenericEnum ABC) instance enumABC :: Enum ABC
-instance enumABC :: Enum ABC where
- succ = genericSucc
- pred = genericPred
Writing those newtypes is straightforward and I gladly volonteer. The only annoyance is how to name them: GenericBounded isn’t taken but GenericShow and GenericEnum refer to the typeclasses implemented by the generic representation.
Would it be acceptable to rename the typeclasses to GenericRep* and keep Generic* for the newtypes? Or has someone a better naming scheme to propose?
I'd quite like to retain the current class names so that we can avoid a breaking change. How about a Using prefix for newtypes? We could potentially even only provide a single newtype here and hang all of the instances onto it:
newtype UsingGeneric a = UsingGeneric a
instance showGeneric :: Generic a => Show (UsingGeneric a) where [...]
instance boundedGeneric :: Generic a => Bounded (UsingGeneric a) where [...]
instance enumGeneric :: Generic a => Enum (UsingGeneric a) where [...]
and then:
data ABC = ABC
derive instance genericABC :: Generic ABC _
derive via (UsingGeneric ABC) instance showABC :: Show ABC
derive via (UsingGeneric ABC) instance boundedABC :: Bounded ABC
derive via (UsingGeneric ABC) instance enumABC :: Enum ABC