[Haskell-cafe] Type classes for converting to Text and String

Simon Hengel sol at typeful.net
Thu Mar 8 10:53:15 CET 2012


When writing library code that should work with both String and Text I
find my self repeatedly introducing classes like:

    class ToString a where
      toString :: a -> String

    class ToText a where
      toText :: a -> Text

(I use this with newtype wrapped value types backed by Text or

So I wonder whether it would be a good idea to have a package that
provides those classes.

Or maybe just ToText, and provide default implementations of toString
and toText, like:

    class ToText a where

      toText :: a -> Text
      toText = Text.pack . toString

      toString :: a -> String
      toString = Text.unpack . toText

How do you guys deal with that?  Any thoughts?


More information about the Haskell-Cafe mailing list