Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[generics-rep] Newtypes for deriving via #241

Open
kl0tl opened this issue Apr 6, 2020 · 1 comment
Open

[generics-rep] Newtypes for deriving via #241

kl0tl opened this issue Apr 6, 2020 · 1 comment
Labels
status: blocked This issue or PR is blocked by something and cannot make progress. type: enhancement A new feature or addition.

Comments

@kl0tl
Copy link
Member

kl0tl commented Apr 6, 2020

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?

@hdgarrood
Copy link
Contributor

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

@JordanMartinez JordanMartinez changed the title Newtypes for deriving via [generics-rep] Newtypes for deriving via Dec 26, 2020
@JordanMartinez JordanMartinez transferred this issue from purescript-deprecated/purescript-generics-rep Dec 26, 2020
@JordanMartinez JordanMartinez added type: enhancement A new feature or addition. status: blocked This issue or PR is blocked by something and cannot make progress. labels Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: blocked This issue or PR is blocked by something and cannot make progress. type: enhancement A new feature or addition.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants