[Haskell-cafe] type/class question: toString

Graham Fawcett graham.fawcett at gmail.com
Thu Nov 8 08:57:27 EST 2007


On Nov 7, 2007 4:34 PM, Nicholas Messenger <nmessenger at gmail.com> wrote:
> If you're willing to have an extra Typeable constraint, this does what you want:
>
> > import Data.Typeable (Typeable, cast)
> > import Data.Maybe (fromMaybe)
> >
> > toString :: (Show a, Typeable a) => a -> String
> > toString x = fromMaybe (show x) (cast x)


Nice! Thank you for introducing me to the Typeable class (and by
extension, Dynamic) -- I hadn't realized there was library support for
dynamic types.

G


More information about the Haskell-Cafe mailing list