John Meacham | 16 May 00:02 2006
Picon

make Text.Printf extensible, more flexible

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
 }

so, a UPrintf is just a function that takes a parsed formating string
and prints the appropriate string.

        John

--

-- 
John Meacham - ⑆repetae.net⑆john⑈
Simon Marlow | 16 May 11:25 2006
Picon

Re: make Text.Printf extensible, more flexible

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.

(Continue reading)


Gmane