feat(users/Profpatsch/my-prelude): show Label name in Show instance
Before: `show (label @"foo" 23) => "Label 23"` Now: `show (label @"foo" 42) => "Label @"foo" 42" Also with good bracketing due to showsPrec (and correct string escaping of the label). Change-Id: Ia5448ab9028ef5ab6c0b53407fe4df1d0e40ff5f Reviewed-on: https://cl.tvl.fyi/c/depot/+/7719 Reviewed-by: Profpatsch <mail@profpatsch.de> Autosubmit: Profpatsch <mail@profpatsch.de> Tested-by: BuildkiteCI
This commit is contained in:
parent
1b003db725
commit
e6862413ca
1 changed files with 10 additions and 2 deletions
|
@ -20,16 +20,24 @@ import Data.Data (Proxy (..))
|
|||
import Data.Function ((&))
|
||||
import Data.Typeable (Typeable)
|
||||
import GHC.Records (HasField (..))
|
||||
import GHC.TypeLits (Symbol)
|
||||
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
|
||||
|
||||
-- | A labelled value.
|
||||
--
|
||||
-- Use 'label'/'label'' to construct,
|
||||
-- then use dot-syntax to get the inner value.
|
||||
newtype Label (label :: Symbol) value = Label value
|
||||
deriving stock (Show, Eq, Ord)
|
||||
deriving stock (Eq, Ord)
|
||||
deriving newtype (Typeable)
|
||||
|
||||
instance (KnownSymbol label, Show value) => Show (Label label value) where
|
||||
showsPrec d (Label val) =
|
||||
showParen (d > 10) $
|
||||
showString "Label @"
|
||||
. showsPrec 11 (symbolVal (Proxy @label))
|
||||
. showString " "
|
||||
. showsPrec 11 val
|
||||
|
||||
-- | Attach a label to a value; should be used with a type application to name the label.
|
||||
--
|
||||
-- @@
|
||||
|
|
Loading…
Reference in a new issue