A typeclass for user-facing output.
Methods
displayBuilder :: a -> BuilderImplement this method to describe how to convert your value to Builder.
displayList :: [a] -> BuilderThe method displayList is provided to allow for a specialised way to render lists of a certain value. This is used to render the list of Char as a string of characters enclosed in double quotes, rather than between square brackets and separated by commas.
Example
import qualified Data.Text.Lazy.Builder as TB instance Display Char where displayBuilder c = TB.fromText $ T.singleton c displayList cs = TB.fromText $ T.pack cs instance (Display a) => Display [a] where -- In this instance, 'displayBuilder' is defined in terms of 'displayList', which for most types -- is defined as the default written in the class declaration. -- But when a ~ Char, there is an explicit implementation that is selected instead, which -- provides the rendering of the character string between double quotes. displayBuilder = displayListHow implementations are selected
displayBuilder ([1,2,3] :: [Int]) → displayBuilder @[Int] = displayBuilderList @Int → Default `displayList` displayBuilder ("abc" :: [Char]) → displayBuilder @[Char] = displayBuilderList @Char → Custom `displayList`displayPrec :: Int -> a -> BuilderThe method displayPrec allows you to write instances that require nesting. The precedence parameter can be thought of as a suggestion coming from the surrounding context for how tightly to bind. If the precedence parameter is higher than the precedence of the operator (or constructor, function, etc.) being displayed, then that suggests that the output will need to be surrounded in parentheses in order to bind tightly enough (see displayParen).
For example, if an operator constructor is being displayed, then the precedence requirement for its arguments will be the precedence of the operator. Meaning, if the argument binds looser than the surrounding operator, then it will require parentheses.
Note that function/constructor application has an effective precedence of 10.
Examples
instance (Display a) => Display (Maybe a) where -- In this instance, we define 'displayPrec' rather than 'displayBuilder' as we need to decide -- whether or not to surround ourselves in parentheses based on the surrounding context. -- If the precedence parameter is higher than 10 (the precedence of constructor application) -- then we indeed need to surround ourselves in parentheses to avoid malformed outputs -- such as @Just Just 5@. -- We then set the precedence parameter of the inner 'displayPrec' to 11, as even -- constructor application is not strong enough to avoid parentheses. displayPrec _ Nothing = "Nothing" displayPrec prec (Just a) = displayParen (prec > 10) $ "Just " <> displayPrec 11 adata Pair a b = a :*: b infix 5 :*: -- arbitrary choice of precedence instance (Display a, Display b) => Display (Pair a b) where displayPrec prec (a :*: b) = displayParen (prec > 5) $ displayPrec 6 a <> " :*: " <> displayPrec 6 b
Instances35Display, …
CannotDisplayByteStrings => Display ByteStringDefined in text-display-0.0.5.2 · Data.Text.Display.Core🚫 You should not try to display strict ByteStrings!
💡 Always provide an explicit encoding. Use decodeUtf8' or decodeUtf8With to convert from UTF-8
CannotDisplayByteStrings => Display ByteStringDefined in text-display-0.0.5.2 · Data.Text.Display.Core🚫 You should not try to display lazy ByteStrings!
💡 Always provide an explicit encoding. Use decodeUtf8' or decodeUtf8With to convert from UTF-8
Display IntegerDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay VoidDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay SomeExceptionDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay IOExceptionDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Int16Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Int32Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Int64Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Int8Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Word16Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Word32Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Word64Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay Word8Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay BoolDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay CharDefined in text-display-0.0.5.2 · Data.Text.Display.CoredisplayList is overloaded, so that when the
Display [a]instance calls displayList, we end up with a nice string instead of a list of chars between brackets.Example1 expression display [1, 2, 3]"[1,2,3]"
Example1 expression display ['h', 'e', 'l', 'l', 'o']"hello"
Display DoubleDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay FloatDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay IntDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay WordDefined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay TextDefined in text-display-0.0.5.2 · Data.Text.Display.CoreStrict Text
Display TextDefined in text-display-0.0.5.2 · Data.Text.Display.CoreLazy Text
Display ()Defined in text-display-0.0.5.2 · Data.Text.Display.CoreRealFloat e => Display (DisplayRealFloat e)Defined in text-display-0.0.5.2 · Data.Text.Display.CoreIntegral e => Display (DisplayDecimal e)Defined in text-display-0.0.5.2 · Data.Text.Display.CoreShow e => Display (ShowInstance e)Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay a => Display (NonEmpty a)Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay a => Display (Maybe a)Defined in text-display-0.0.5.2 · Data.Text.Display.CoreDisplay a => Display [a]Defined in text-display-0.0.5.2 · Data.Text.Display.Core(AssertNoSumRecordInstance Display a, Generic a, GDisplay1 (Rep a)) => Display (RecordInstance a)Defined in text-display-0.0.5.2 · Data.Text.Display.GenericWe leverage the
AssertNoSumtype family to prevent consumers from deriving instances for sum types. Sum types should use a manual instance or derive one via ShowInstance.KnownSymbol str => Display (OpaqueInstance str a)Defined in text-display-0.0.5.2 · Data.Text.Display.CoreThis wrapper allows you to create an opaque instance for your type, useful for redacting sensitive content like tokens or passwords.
CannotDisplayBareFunctions => Display (a -> b)Defined in text-display-0.0.5.2 · Data.Text.Display.Core🚫 You should not try to display functions!
💡 Write a
newtypewrapper that represents your domain more accurately. If you are not consciously trying to use display on a function, make sure that you are not missing an argument somewhere.(Display a, Display b) => Display (a, b)Defined in text-display-0.0.5.2 · Data.Text.Display.Core(Display a, Display b, Display c) => Display (a, b, c)Defined in text-display-0.0.5.2 · Data.Text.Display.Core(Display a, Display b, Display c, Display d) => Display (a, b, c, d)Defined in text-display-0.0.5.2 · Data.Text.Display.Core