[generics-rep] Newtypes for deriving via

Abierto
#241 1 comentario 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

  • #37 de @kl0tl — cerrado sin fusionar

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
20/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Área
backend

Línea de trabajo

Read the issue’s deriving-via examples and the linked PureScript PR #3824 to understand the proposed mechanism. The issue asks for naming guidance for newtypes such as GenericBounded, GenericShow, and GenericEnum; done would mean a naming scheme is agreed on, but the thread has no recorded decision and a linked pull request is closed.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

status: blocked type: enhancement

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?

Lenguaje dominante
PureScript
Estrellas
168
Forks
90
Merge medio
3 d 1 h
PR fusionados (30 d)
1

Preparar el entorno

  • Sin Dockerfile ni archivo de Docker Compose
  • Tiene una plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de purescript/purescript-prelude

Todos los issues de purescript/purescript-prelude

Issues similares

Más issues de Backend & API Design

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.