[Haskell-cafe] type/class question: toString

Graham Fawcett graham.fawcett at gmail.com
Tue Nov 6 14:08:03 EST 2007


On Nov 6, 2007 12:03 PM, Thomas Schilling <nominolo at googlemail.com> wrote:
> On Tue, 2007-11-06 at 09:18 -0500, Graham Fawcett wrote:
> > Hi folks,
> > Is there a way to declare a 'toString' function, such that
> > toString x | <x is a String> = x
> > toString x | <x's type is an instance of Show> = show x
> I think the simpler solution (for your particular problem) is to tag
> strings that should be printed as-is:
>
> newtype Literal = Literal String
> instance Show Literal where show (Literal x) = x
> lit :: String -> Literal
> lit = Literal

I almost replied to ask, "doesn't that solve a different problem?" But
I see that in practice, it leads to a similar result, and without
type-system trickery.

The type-system trickery is still devilishly interesting, though. ;-)

Thanks,
G


More information about the Haskell-Cafe mailing list