Skip to content

[generics-rep] Newtypes for deriving via #241

Open
@kl0tl

Description

@kl0tl

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: blockedThis issue or PR is blocked by something and cannot make progress.type: enhancementA new feature or addition.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions