display-0.0.1: Display things for humans to read

Safe HaskellSafe
LanguageHaskell2010

Display

Contents

Description

Display human-readable values to the user efficiently. See the display method for documentation.

Use the OverloadedStrings language extension when using this module to produce Builder values conveniently e.g. "Hello" <> display name.

Synopsis

Printing method

display :: Display a => a -> Builder Source #

Display a value in a human-readable format. This is the opposite of the Show class which is intended for programmers to read. See Display for how to use this for your own types.

  • Writing to file or output: The Builder can be written directly to a handle with hPutBuilder very efficiently as UTF-8. This is the preferred method when writing to stdout or a file.
  • Writing to a string: Use one of the functions in Conversions like displayByteString or displayString (when needed).

Use the functions in Data.ByteString.Builder for hex encodings of numbers and strings.

To append Builder you can use the Monoid instance, for example to print things comma-separated, you can use intersperse and mconcat:

mconcat (intersperse ", " (map display [1, 4, 5]))

This example requires the OverloadedStrings language extension.

Conversions

displayByteString :: Display a => a -> ByteString Source #

Display to a ByteString value.

displayLazyByteString :: Display a => a -> ByteString Source #

Display to a ByteString value.

displayString :: Display a => a -> String Source #

Display to a String value. Very inefficient. Only use when you are forced to by another API.

displayText :: Display a => a -> Text Source #

Display to a Text value. Inefficient. Only use when you have to.

displayLazyText :: Display a => a -> Text Source #

Display to a Text value. Inefficient. Only use when you have to.

The class

class Display a where Source #

Display a value in a human-readable format. This is the opposite of the Show class which is intended for programmers to read, and can be used alongside Show.

For example, consider: Maybe String

>>> show (Just "abc")
Just "abc"
>>> show Nothing
Nothing

whereas display is meant for printing to users, so you might write this:

>>> display (Just "abc")
abc
>>> display Nothing
""

You can safely use newtype deriving with this type, e.g.

newtype Name = Name Text deriving (Show, Display)

Instances for exceptions can be written like this:

data MyException = SomeProblem deriving (Show, Typeable)
instance Exception MyException where displayException = displayString

Minimal complete definition

display

Methods

display :: a -> Builder Source #

Display a value in a human-readable format. This is the opposite of the Show class which is intended for programmers to read. See Display for how to use this for your own types.

  • Writing to file or output: The Builder can be written directly to a handle with hPutBuilder very efficiently as UTF-8. This is the preferred method when writing to stdout or a file.
  • Writing to a string: Use one of the functions in Conversions like displayByteString or displayString (when needed).

Use the functions in Data.ByteString.Builder for hex encodings of numbers and strings.

To append Builder you can use the Monoid instance, for example to print things comma-separated, you can use intersperse and mconcat:

mconcat (intersperse ", " (map display [1, 4, 5]))

This example requires the OverloadedStrings language extension.

Instances

Display Char Source # 

Methods

display :: Char -> Builder Source #

Display Double Source # 
Display Float Source # 
Display Int Source # 

Methods

display :: Int -> Builder Source #

Display Int8 Source # 

Methods

display :: Int8 -> Builder Source #

Display Int16 Source # 
Display Int32 Source # 
Display Int64 Source # 
Display Integer Source # 
Display Word Source # 

Methods

display :: Word -> Builder Source #

Display Word8 Source # 
Display Word16 Source # 
Display Word32 Source # 
Display Word64 Source # 
Display ByteString Source # 
Display ByteString Source # 
Display Builder Source # 
Display Text Source # 

Methods

display :: Text -> Builder Source #

Display Text Source # 

Methods

display :: Text -> Builder Source #

Display [Char] Source # 

Methods

display :: [Char] -> Builder Source #

Display a => Display (Maybe a) Source # 

Methods

display :: Maybe a -> Builder Source #

(Display a, Display b) => Display (Either a b) Source # 

Methods

display :: Either a b -> Builder Source #