From e6862413ca032acc94615bd969c8fec49a1a1dc5 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Mon, 2 Jan 2023 01:34:23 +0100 Subject: [PATCH] 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 Autosubmit: Profpatsch Tested-by: BuildkiteCI --- users/Profpatsch/my-prelude/Label.hs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/users/Profpatsch/my-prelude/Label.hs b/users/Profpatsch/my-prelude/Label.hs index f869343a1..0e339758d 100644 --- a/users/Profpatsch/my-prelude/Label.hs +++ b/users/Profpatsch/my-prelude/Label.hs @@ -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. -- -- @@