[Haskell-cafe] Type classes for converting to Text and String
Yves Parès
yves.pares at gmail.com
Thu Mar 8 12:37:31 CET 2012
If you just need to go back and forth from String to Text, why do you need
to be generic? pack and unpack from Data.Text do the job.
Plus, in the way of what Christopher said, you can use the
OverloadedStrings extension. You can then use the string syntax at a place
that expects a text:
{-# LANGUAGE OverloadedStrings #-}
import Data.Text
t :: Text
t = "Hello"
Any instance of the IsString class can be used in this way, not only Text.
2012/3/8 Simon Hengel <sol at typeful.net>
> Hi!
>
> 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
> ByteString.)
>
> 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?
>
> Cheers,
> Simon
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120308/13762baa/attachment.htm>
More information about the Haskell-Cafe
mailing list