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 <s...@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@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe >
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe