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

Simon Hengel sol at typeful.net
Thu Mar 8 11:42:35 CET 2012


On Thu, Mar 08, 2012 at 11:00:34AM +0100, Christopher Done wrote:
> On 8 March 2012 10:53, Simon Hengel <sol at typeful.net> wrote:
> > 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
> 
> Text is already an instance of IsString which provides IsString.

What exactly do you mean?

> I've defined ToString in my own projects though, it would be nice for
> it to be defined somewhere (Data.String maybe?).

We could write a proposal to add ToString to base (maybe a good idea,
not sure).  ToString has a striking similarity with Show, but it's still
different:

 * toString converts some a to a String
 * show gives a string _representation_ of some a

(e.g. converting a String to a String is just id and hence different
from show; this is akin to Python's __str__/__repr__)

But this does still not help with toText.

Cheers,
Simon



More information about the Haskell-Cafe mailing list