
On 25 May 2011 22:52, Johan Tibell
On Wed, May 25, 2011 at 2:01 PM, Ivan Lazar Miljenovic
wrote: With my wl-pprint-text package, Jason Dagit suggested to me on #haskell that it would make sense to make such a pretty-printer be class-based so that the same API could be used for String, ByteString, Text, etc.
I'm a bit skeptical of using type classes to abstract over Unicode string types and byte sequence types. The only API shared by the two kind of types is that of a sequence. Things like dot , spaces, etc. don't make much sense on binary data. You must assume that the ByteString contains text in some encoding to make sense of such concepts.
Well, using the Char8 version. I was thinking of a class looking something like: class (IsString s, Monoid (Builder s)) => Prettyable s where type Builder s toBuilder :: s -> Builder s fromBuilder :: Builder s -> s And then Doc now takes in a type parameter, which needs to be an instance of Prettyable. The IsString constraint is so that combinators like "int" and "double" can be defined via show (rather than have them being class-level methods), toBuilder is used to convert an actual s value to (Doc s) and fromBuilder is used for the final rendering at the end. The only other thing I can think of that _may_ need to be done on a class level is the allowable size lengths of values (Int64 for lazy ByteString and Text, and possibly just Int for convenience with String). Though if displayIO is going to be kept some kind of "s -> IO ()" method will also be needed, though arguably displayIO isn't that necessary. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com IvanMiljenovic.wordpress.com