from-string-1.0.0.2: Instances of 'From' for common string types
Copyright(C) 2024-2025 XT et al.
LicenseApache-2.0 (see the LICENSE file)
PortabilityFlexibleInstances, MultiParamTypeClasses
Safe HaskellSafe-Inferred
LanguageHaskell2010

From.String

Contents

Description

If you are an experienced Haskeller, you'd immediately guess what this is about. Your guess is correct. There are 8 types covered by this module:

  • String (legacy)
  • Sequence of bytes

    • Strict
    • Lazy
    • Builder
    • Unpinned
  • Sequence of Unicode characters

    • Strict
    • Lazy
    • Builder

Check the implementation, and go enjoy the convenience of from.

But if you are not familiar with the history of how Haskell dealt with the concept of string (whatever it means), here's a bit of introduction:

Introduction

Haskell has String, but Haskellers have been not entirely happy with it. You probably shouldn't rely on it either. It's a legacy type. Please kindly understand the situation; Haskell is older than Unicode. Haskell is older than the WWW itself.

There are alternatives in the modern Haskell ecosystem. The two major relevant packages are:

  1. "bytestring" for "sequences of bytes" where a "byte" is any value from 0x00 to 0xFF.
  2. "text" for sequences of Unicode characters.

Each package offers "strict" and "lazy" variants. If you have no idea what this means, you can just start using the strict ones (ByteString and Text). They are just like the immutable contiguous byte/character sequence types commonly found in other programming languages.

There are also "builder" types for efficient construction (eventual concatenation or output) of sequences: Builder (bytestring) and Builder (text).

ByteString is pinned in the memory. This means the GC cannot move it around. For large data, this is the desired behavior, but for small sequences of bytes, this can lead to memory fragmentation. For that reason, there is an unpinned variant called ShortByteString. If you still don't understand, just stick to ByteString for now.

So that's 8 distinct types to deal with. It is difficult to remember the conversion functions. This module aims to provide the "practical defaults" for that, so that you can simply use from in most cases.

Each instance's documentation in the instances section reveals the underlying implementation. If you need a different assumption (e.g. conversion not in UTF-8), you should express the accurate conversion in your code, instead of from.

Synopsis

Instances

class From a b where #

The type conversion class where conversion is (mostly) safe and expected to be successful.

Methods

from :: a -> b #

Instances

Instances details
From Builder ByteString Source #

toStrict . toLazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> ByteString #

From Builder ByteString Source #

toLazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> ByteString #

From Builder ShortByteString Source #

toShort . toStrict . toLazyByteString

Instance details

Defined in From.String.AutoGen

From Builder Text Source #

decodeUtf8Lenient . toStrict . toLazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> Text #

From Builder Builder Source #

fromLazyText . decodeUtf8With lenientDecode . toLazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> Builder #

From Builder Text Source #

decodeUtf8With lenientDecode . toLazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> Text #

From Builder String Source #

unpack . decodeUtf8With lenientDecode . toLazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> String #

From ByteString Builder Source #

byteString

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Builder #

From ByteString ByteString Source #

fromStrict

Instance details

Defined in From.String.AutoGen

From ByteString ShortByteString Source #

toShort

Instance details

Defined in From.String.AutoGen

From ByteString Text Source #

decodeUtf8Lenient

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Text #

From ByteString Builder Source #

fromText . decodeUtf8Lenient

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Builder #

From ByteString Text Source #

decodeUtf8With lenientDecode . fromStrict

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Text #

From ByteString String Source #

unpack . decodeUtf8Lenient

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> String #

From ByteString Builder Source #

lazyByteString

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Builder #

From ByteString ByteString Source #

toStrict

Instance details

Defined in From.String.AutoGen

From ByteString ShortByteString Source #

toShort . toStrict

Instance details

Defined in From.String.AutoGen

From ByteString Text Source #

decodeUtf8Lenient . toStrict

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Text #

From ByteString Builder Source #

fromLazyText . decodeUtf8With lenientDecode

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Builder #

From ByteString Text Source #

decodeUtf8With lenientDecode

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> Text #

From ByteString String Source #

unpack . decodeUtf8With lenientDecode

Instance details

Defined in From.String.AutoGen

Methods

from :: ByteString -> String #

From ShortByteString Builder Source #

shortByteString

Instance details

Defined in From.String.AutoGen

From ShortByteString ByteString Source #

fromShort

Instance details

Defined in From.String.AutoGen

From ShortByteString ByteString Source #

fromStrict . fromShort

Instance details

Defined in From.String.AutoGen

From ShortByteString Text Source #

decodeUtf8Lenient . fromShort

Instance details

Defined in From.String.AutoGen

Methods

from :: ShortByteString -> Text #

From ShortByteString Builder Source #

fromText . decodeUtf8Lenient . fromShort

Instance details

Defined in From.String.AutoGen

From ShortByteString Text Source #

decodeUtf8With lenientDecode . fromStrict . fromShort

Instance details

Defined in From.String.AutoGen

Methods

from :: ShortByteString -> Text #

From ShortByteString String Source #

unpack . decodeUtf8Lenient . fromShort

Instance details

Defined in From.String.AutoGen

From Text Builder Source #

encodeUtf8Builder

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> Builder #

From Text ByteString Source #

encodeUtf8

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> ByteString #

From Text ByteString Source #

encodeUtf8 . fromStrict

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> ByteString #

From Text ShortByteString Source #

toShort . encodeUtf8

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> ShortByteString #

From Text Builder Source #

fromText

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> Builder #

From Text Text Source #

fromStrict

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> Text #

From Text String Source #

unpack

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> String #

From Builder Builder Source #

encodeUtf8Builder . toLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> Builder #

From Builder ByteString Source #

toStrict . encodeUtf8 . toLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> ByteString #

From Builder ByteString Source #

encodeUtf8 . toLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> ByteString #

From Builder ShortByteString Source #

toShort . toStrict . encodeUtf8 . toLazyText

Instance details

Defined in From.String.AutoGen

From Builder Text Source #

toStrict . toLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> Text #

From Builder Text Source #

toLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> Text #

From Builder String Source #

unpack . toLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Builder -> String #

From Text Builder Source #

encodeUtf8Builder

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> Builder #

From Text ByteString Source #

toStrict . encodeUtf8

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> ByteString #

From Text ByteString Source #

encodeUtf8

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> ByteString #

From Text ShortByteString Source #

toShort . toStrict . encodeUtf8

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> ShortByteString #

From Text Text Source #

toStrict

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> Text #

From Text Builder Source #

fromLazyText

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> Builder #

From Text String Source #

unpack

Instance details

Defined in From.String.AutoGen

Methods

from :: Text -> String #

From String Builder Source #

stringUtf8

Instance details

Defined in From.String.AutoGen

Methods

from :: String -> Builder #

From String ByteString Source #

encodeUtf8 . pack

Instance details

Defined in From.String.AutoGen

Methods

from :: String -> ByteString #

From String ByteString Source #

encodeUtf8 . pack

Instance details

Defined in From.String.AutoGen

Methods

from :: String -> ByteString #

From String ShortByteString Source #

toShort . encodeUtf8 . pack

Instance details

Defined in From.String.AutoGen

From String Text Source #

pack

Instance details

Defined in From.String.AutoGen

Methods

from :: String -> Text #

From String Builder Source #

fromString

Instance details

Defined in From.String.AutoGen

Methods

from :: String -> Builder #

From String Text Source #

pack

Instance details

Defined in From.String.AutoGen

Methods

from :: String -> Text #