[core libraries] Data.Display - Textual representation of Haskell values (was Re: PROPOSAL: Add displayException to Exception typeclass)

Michael Snoyman michael at snoyman.com
Mon Nov 10 18:32:12 UTC 2014


On Mon Nov 10 2014 at 8:28:18 PM Michael Snoyman <michael at snoyman.com>
wrote:

> On Mon Nov 10 2014 at 7:17:07 PM Simon Hengel <sol at typeful.net> wrote:
>
>> > > So I would love to have tho following type class:
>> > >
>> > >     class Display a where
>> > >       display :: a -> String
>> > >       default display :: Show a => a -> String
>> > >       display = show
>> > >
>> > > Note that for many Prelude types `show == display`, with the notable
>> > > exception of String, where `display = id`.  One use case where this
>> > > matters is string interpolation [1][2][3].
>> >
>> > I would also like a class like that to exist.
>> >
>> > But I think it should be based on Text and Text builder rather than
>> > String (and hence be outside of base).
>>
>> I created an initial draft:
>>
>>     https://github.com/sol/display/blob/master/src/Data/Display.hs
>>
>>
> Is this intended to be a standalone library, or part of text (or some
> other package?).
>
>
>> I named the builder version `displayBuilder` as I could not come up with
>> any better name (suggestions much appreciated!).
>>
>> In addition, I think the `display` method has to be outside of the
>> class.  Otherwise, if somebody defines `display` he end up with the
>> default implementation of `displayBuilder` (which is based on show and
>> may not be what is intended).  Any input on that?
>>
>>
> I don't think this is a good enough reason to avoid a possible
> optimization. I'd recommend documenting this aspect of the typeclass
> thoroughly, and then let people shoot themselves in the foot if desired.
>
> Michael
>
> PS: I just saw Greg's email, I'd like to +1 his questions, clarification
> is good.
>

One other question/comment: I'm not convinced that the `String` instance is
a good thing. It will conflict with any other list instance, unless you
turn on OverlappingInstances (which I hope you don't). You could use the
`showList` hack, but we should really clarify first what the intended list
instance is.

Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/libraries/attachments/20141110/eea296a2/attachment.html>


More information about the Libraries mailing list