make Text.Printf extensible, more flexible

Simon Marlow simonmarhaskell at
Tue May 16 05:25:55 EDT 2006

John Meacham wrote:
> I was wondering if we could make Text.Printf extensible to user defined
> types.
> as far as I can tell, all that is needed is to export PrintfArg so that
> users can define new instances. it would also be nice if UPrintf could
> be switched from the current union type to a higher order function,
> since the only thing UPrintf is used for is formating strings. I am
> thinking something like
> newtype UPrintf = UPrintf (UFmt -> ShowS)
> data Adjustment = LeftAdjust | ZeroPad
> data UFmt = UFmt {
>         fmtAdjust :: Maybe Adjustment,
>         fmtFieldWidth :: Maybe Int,
>         fmtPrecision :: Maybe Int,
>         fmtCharacter :: Char
>  }

Or do away with UPrintf completely:

class PrintfArg a where
   formatArg :: a -> UFmt -> Maybe ShowS

(in other words, type UPrintf = UFmt -> Maybe ShowS).  It must return a 
Maybe because the formatting char might be wrong.

We would probably have to supply some combinators for building PrintfArg 
instances, they share a lot of code.


More information about the Libraries mailing list