HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletext-conversions-0.3.1.1Haskell2010

Data.Text.Conversions

This module provides a set of typeclasses for safely converting between textual data. The built-in String type, as well as strict Text and lazy Text, are safely convertible between one another. The ByteString type is frequently treated in much the same manner, but this is unsafe for two reasons:

  • Since ByteString encodes binary data, it does not specify a particular encoding, so assuming a particular encoding like UTF-8 would be incorrect.

  • Furthermore, decoding binary data into text given a particular encoding can fail. Most systems simply use decodeUtf8 and similar functions, which will dangerously throw exceptions when given invalid data.

This module addresses both problems by providing a DecodeText typeclass for decoding binary data in a way that can fail and by providing a UTF8 wrapper type for selecting the desired encoding.

Most of the time, you will not need to create your own instances or use the underlying functions that make the conversion machinery tick. Instead, just use the convertText method to convert between two textual datatypes or the decodeConvertText method to perform a conversion that can fail.

Examples:

Example3 expressions
convertText ("hello" :: String) :: Text"hello"decodeConvertText (UTF8 ("hello" :: ByteString)) :: Maybe TextJust "hello"decodeConvertText (UTF8 ("\xc3\x28" :: ByteString)) :: Maybe TextNothing
  • 3 types
  • 3 classes
  • 2 values

Conversion typeclasses and functions

5 declarations
classclass FromText a where
#

A simple typeclass that handles converting Text to arbitrary datatypes. If you have a type that can be produced from text, implement this typeclass. However, you probably do not want to call fromText directly; call convertText, instead.

Methods

Instances9FromText, …
classclass ToText a where
#

A simple typeclass that handles converting arbitrary datatypes to Text when the operation cannot fail. If you have a type that satisfies that requirement, implement this typeclass, but if the operation can fail, use DecodeText instead.

Methods

Instances7ToText, …
classclass Functor f => DecodeText (f :: Type -> Type) a where
#

A simple typeclass that handles converting arbitrary datatypes to Text when the operation can fail. If you have a type that satisfies that requirement, implement this typeclass, but if the operation cannot fail, use ToText instead.

Methods

Instances2DecodeText
valueconvertText :: (ToText a, FromText b) => a -> b
#

A function that provides a way to safely convert between arbitrary textual datatypes where the conversion to text cannot fail.

Example1 expression
convertText ("hello" :: String) :: Text"hello"
valuedecodeConvertText :: (DecodeText f a, FromText b) => a -> f b
#

A function that provides a way to safely convert between arbitrary textual datatypes where the conversion to text can fail, such as decoding binary data to text. Since binary data can represent text in many different potential encodings, it is necessary to use a newtype that picks the particular encoding, like UTF8:

Example1 expression
decodeConvertText (UTF8 ("hello" :: ByteString)) :: Maybe TextJust "hello"

Encoding newtypes

3 declarations
newtypenewtype UTF8 a
#

Simple wrapper type that is used to select a desired encoding when encoding or decoding text from binary data, such as ByteStrings. The conversion is not partial; it will result in Nothing when a ByteString is provided with data that is not valid in UTF-8.

Example3 expressions
convertText ("hello" :: Text) :: UTF8 ByteStringUTF8 "hello"decodeConvertText (UTF8 ("hello" :: ByteString)) :: Maybe TextJust "hello"decodeConvertText (UTF8 ("invalid \xc3\x28" :: ByteString)) :: Maybe TextNothing

Constructors

Instances7Functor, DecodeText, Eq, Show, FromText, …
newtypenewtype Base16 a
#

Wrapper type used to select a base 16 encoding when encoding or decoding binary data. Safe because base 16 encoding will always produce ASCII output.

Constructors

Instances7Functor, Eq, Show, FromText, ToText, …
newtypenewtype Base64 a
#

Wrapper type used to select a base 64 encoding when encoding or decoding binary data. Safe because base 64 encoding will always produce ASCII output.

Constructors

Instances7Functor, Eq, Show, FromText, ToText, …