Type classes for converting to Text and String

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

On 8 March 2012 10:53, Simon Hengel
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. I've defined ToString in my own projects though, it would be nice for it to be defined somewhere (Data.String maybe?).

On Thu, Mar 08, 2012 at 11:00:34AM +0100, Christopher Done wrote:
On 8 March 2012 10:53, Simon Hengel
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

* Simon Hengel
When writing library code that should work with both String and Text I find my self repeatedly introducing classes like: [...] How do you guys deal with that? Any thoughts?
If it's fine to depend on FunDeps, you can use ListLike. http://hackage.haskell.org/package/ListLike -- Roman I. Cheplyaka :: http://ro-che.info/

On Thu, Mar 08, 2012 at 12:18:56PM +0200, Roman Cheplyaka wrote:
If it's fine to depend on FunDeps, you can use ListLike. http://hackage.haskell.org/package/ListLike
How would that help with toText? Cheers, Simon

* Simon Hengel
On Thu, Mar 08, 2012 at 12:18:56PM +0200, Roman Cheplyaka wrote:
If it's fine to depend on FunDeps, you can use ListLike. http://hackage.haskell.org/package/ListLike
How would that help with toText?
toText = fromListLike (ListLike instance for Text is provided by the listlike-instances package.) -- Roman I. Cheplyaka :: http://ro-che.info/

On Thu, Mar 08, 2012 at 12:54:13PM +0200, Roman Cheplyaka wrote:
* Simon Hengel
[2012-03-08 11:48:41+0100] On Thu, Mar 08, 2012 at 12:18:56PM +0200, Roman Cheplyaka wrote:
If it's fine to depend on FunDeps, you can use ListLike. http://hackage.haskell.org/package/ListLike
How would that help with toText?
toText = fromListLike
(ListLike instance for Text is provided by the listlike-instances package.)
Ah, the listlike-instances package is the missing piece. Not sure if this is going somewhere. But I'm still trying to get a clear picture of the performance implications. Say I have a newtype-wrapped ByteString that I would decode to String/Text using UTF-8: newtype Value = Value ByteString Would it be possible to go from Value to Text by essentially ending up with Data.Text.Encoding.decodeUtf8 at runtime (e.g. by using rewrite rules)? Cheers, Simon

* Simon Hengel
On Thu, Mar 08, 2012 at 12:54:13PM +0200, Roman Cheplyaka wrote:
* Simon Hengel
[2012-03-08 11:48:41+0100] On Thu, Mar 08, 2012 at 12:18:56PM +0200, Roman Cheplyaka wrote:
If it's fine to depend on FunDeps, you can use ListLike. http://hackage.haskell.org/package/ListLike
How would that help with toText?
toText = fromListLike
(ListLike instance for Text is provided by the listlike-instances package.)
Ah, the listlike-instances package is the missing piece.
Not sure if this is going somewhere. But I'm still trying to get a clear picture of the performance implications.
Say I have a newtype-wrapped ByteString that I would decode to String/Text using UTF-8:
newtype Value = Value ByteString
Would it be possible to go from Value to Text by essentially ending up with Data.Text.Encoding.decodeUtf8 at runtime (e.g. by using rewrite rules)?
You can do that, but it will work only if your functions are specialized enough at compile time. -- Roman I. Cheplyaka :: http://ro-che.info/

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
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Thu, Mar 08, 2012 at 12:37:31PM +0100, Yves Parès wrote:
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.
Always going through String or Text may (depending on what your underlying representation is) be less efficient than converting directly to String/Text. Cheers, Simon
participants (4)
-
Christopher Done
-
Roman Cheplyaka
-
Simon Hengel
-
Yves Parès