quiet-0.2: Generic deriving of Read/Show with no record labels.

Safe HaskellSafe
LanguageHaskell2010

Quiet

Description

Generic deriving of Read / Show with no record labels.

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)
>>> show (UserId "simon")
UserId {unUserId = "simon"}
>>> 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)
>>> show (UserId "simon")
UserId "simon"
>>> 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
Synopsis

Documentation

newtype Quiet a Source #

Derive Read / Show using DerivingVia.

Constructors

Quiet 

Fields

Instances
(Generic a, QRead (Rep a)) => Read (Quiet a) Source # 
Instance details

Defined in Quiet

(Generic a, QShow (Rep a)) => Show (Quiet a) Source # 
Instance details

Defined in Quiet

Methods

showsPrec :: Int -> Quiet a -> ShowS #

show :: Quiet a -> String #

showList :: [Quiet a] -> ShowS #

qshowsPrec :: (Generic a, QShow (Rep a)) => Int -> a -> ShowS Source #

This implements a quiet version of showsPrec which omits labels for record fields when rendering constructors.

qreadPrec :: (Generic a, QRead (Rep a)) => ReadPrec a Source #

This implements a quiet version of readPrec which expects labels for record fields to be omitted when parsing constructors.