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

Modulerelude-1.2.0.0Haskell2010

Relude.String.Conversion

SPDX-License-Identifier : MIT Maintainer : Kowainik xrom.xkov@gmail.com Stability : Stable Portability : Portable

This module implements type class which allow to have conversion to and from Text, String and ByteString types (including both strict and lazy versions). Usually you need to export Text modules qualified and use pack / unpack functions to convert to/from Text. Now you can just use toText / toString functions.

  • 2 types
  • 5 classes
  • 4 values
  • Packagerelude-1.2.0.0
  • Exports11
  • LanguageHaskell2010
  • LicenceMIT
  • SourceConversion.hs

Convenient type aliases

2 declarations

Conversion type classes

7 declarations
classclass ConvertUtf8 a b where
#

Type class for conversion to utf8 representation of text.

Methods

  • encodeUtf8 :: a -> b

    Encode as utf8 string (usually B.ByteString).

    Example1 expression
    encodeUtf8 @Text @ByteString "патак""\208\191\208\176\209\130\208\176\208\186"
  • decodeUtf8 :: b -> a

    Decode from utf8 string.

    Example2 expressions
    decodeUtf8 @Text @ByteString "\208\191\208\176\209\130\208\176\208\186""\1087\1072\1090\1072\1082"putTextLn $ decodeUtf8 @Text @ByteString "\208\191\208\176\209\130\208\176\208\186"патак
  • decodeUtf8Strict :: b -> Either UnicodeException a

    Decode as utf8 string but returning execption if byte sequence is malformed.

    Example1 expression
    decodeUtf8 @Text @ByteString "\208\208\176\209\130\208\176\208\186""\65533\1072\1090\1072\1082"
    Example1 expression
    decodeUtf8Strict @Text @ByteString "\208\208\176\209\130\208\176\208\186"Left Cannot decode byte '\xd0': Data.Text.Internal.Encoding: Invalid UTF-8 stream
Instances9ConvertUtf8, …
classclass ToText a where
#

Type class for converting other strings to Text.

Methods

Instances6ToText
  • EncodingError ToText "ByteString" "Text" => ToText ByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toText ("some string" :: ByteString)...... Type 'ByteString' doesn't have instance of 'ToText'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: ByteString -> Text          decodeUtf8Strict :: ByteString -> Either UnicodeException Text...
  • EncodingError ToText "LByteString" "Text" => ToText LByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toText ("some string" :: LByteString)...... Type 'LByteString' doesn't have instance of 'ToText'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: LByteString -> Text          decodeUtf8Strict :: LByteString -> Either UnicodeException Text...
  • EncodingError ToText "ShortByteString" "Text" => ToText ShortByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toText ("some string" :: ShortByteString)...... Type 'ShortByteString' doesn't have instance of 'ToText'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: ShortByteString -> Text          decodeUtf8Strict :: ShortByteString -> Either UnicodeException Text...
  • ToText StringDefined in relude-1.2.0.0 · Relude.String.Conversion
  • ToText LTextDefined in relude-1.2.0.0 · Relude.String.Conversion
  • ToText TextDefined in relude-1.2.0.0 · Relude.String.Conversion
classclass ToLText a where
#

Type class for converting other strings to Text.

Methods

Instances6ToLText
  • EncodingError ToLText "ByteString" "LText" => ToLText ByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toLText ("some string" :: ByteString)...... Type 'ByteString' doesn't have instance of 'ToLText'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: ByteString -> LText          decodeUtf8Strict :: ByteString -> Either UnicodeException LText...
  • EncodingError ToLText "LByteString" "LText" => ToLText LByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toLText ("some string" :: LByteString)...... Type 'LByteString' doesn't have instance of 'ToLText'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: LByteString -> LText          decodeUtf8Strict :: LByteString -> Either UnicodeException LText...
  • EncodingError ToLText "ShortByteString" "LText" => ToLText ShortByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toLText ("some string" :: ShortByteString)...... Type 'ShortByteString' doesn't have instance of 'ToLText'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: ShortByteString -> LText          decodeUtf8Strict :: ShortByteString -> Either UnicodeException LText...
  • ToLText StringDefined in relude-1.2.0.0 · Relude.String.Conversion
  • ToLText TextDefined in relude-1.2.0.0 · Relude.String.Conversion
  • ToLText TextDefined in relude-1.2.0.0 · Relude.String.Conversion
classclass ToString a where
#

Type class for converting other strings to String.

Methods

Instances6ToString
  • EncodingError ToString "ByteString" "String" => ToString ByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toString ("some string" :: ByteString)...... Type 'ByteString' doesn't have instance of 'ToString'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: ByteString -> String          decodeUtf8Strict :: ByteString -> Either UnicodeException String...
  • EncodingError ToString "LByteString" "String" => ToString LByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toString ("some string" :: LByteString)...... Type 'LByteString' doesn't have instance of 'ToString'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: LByteString -> String          decodeUtf8Strict :: LByteString -> Either UnicodeException String...
  • EncodingError ToString "ShortByteString" "String" => ToString ShortByteStringDefined in relude-1.2.0.0 · Relude.String.Conversion

    ⚠️CAUTION⚠️ This instance is for custom error display only.

    You should always specify encoding of bytes explicitly.

    In case it is used by mistake, the user will see the following:

    Example1 expression
    toString ("some string" :: ShortByteString)...... Type 'ShortByteString' doesn't have instance of 'ToString'.      Use 'decodeUtf8' or 'decodeUtf8Strict' to convert from UTF-8:          decodeUtf8       :: ShortByteString -> String          decodeUtf8Strict :: ShortByteString -> Either UnicodeException String...
  • ToString StringDefined in relude-1.2.0.0 · Relude.String.Conversion
  • ToString LTextDefined in relude-1.2.0.0 · Relude.String.Conversion
  • ToString TextDefined in relude-1.2.0.0 · Relude.String.Conversion

Show and read functions

2 declarations
valuereadEither :: Read a => String -> Either Text a
#

Version of readEither that returns Text in case of the parse error.

Example2 expressions
readEither @Int "123"Right 123readEither @Int "aa"Left "Prelude.read: no parse"
valueshow :: (Show a, IsString b) => a -> b
#

Generalized version of show. Unlike show this function is polymorphic in its result type. This makes it more convenient to work with data types like Text or ByteString. However, if you pass the result of show to a function that expects polymorphic argument, this can break type inference, so use -XTypeApplications to specify the textual type explicitly.

Example3 expressions
show (42 :: Int)"42"show (42 :: Double)"42.0"print (show @Text True)"True"