@hackage / quiet

Generic deriving of Read/Show with no record labels.

Latest0.2

About

Metadata

  • Last updated , by JacobStanley
  • License BSD-3-Clause
  • Categories Generics
  • Maintained by: jacob@stanley.io

  • Lottery factor: 0

Links

Installation

Tested Compilers

  1. 8.8.1
  2. 8.6.5
  3. 8.4.4
  4. 8.2.2
  5. 8.0.2

Readme

quiet

Generic deriving of Read / Show with no record labels.

Hackage Travis

Often one wants to create a newtype which has a convenient field accessor like unUserId below, but that unfortunately makes the Show instance which is derived overly verbose.

For example:

newtype UserId = UserId { unUserId :: String }
  deriving (Read, Show)
ghci> show (UserId "simon")
UserId {unUserId = "simon"}
ghci> read "UserId {unUserId = \"simon\"}" :: UserId
UserId {unUserId = "simon"}

With DerivingVia Quiet you can have a Show instance which doesn't print the field labels. It will render as if the unUserId accessor wasn't present at all.

newtype UserId = UserId { unUserId :: String }
  deriving (Generic)
  deriving (Read, Show) via (Quiet UserId)
ghci> show (UserId "simon")
UserId "simon"
ghci> read "UserId \"simon\"" :: UserId
UserId "simon"

If you want to derive Read / Show without using DerivingVia then you can use qreadPrec and qshowsPrec directly.

instance Read UserId where readPrec = qreadPrec
instance Show UserId where showsPrec = qshowsPrec