Skip to content

Printf: function for parsing and checking the format string

Motivation

Briefly describe the problem your proposal solves and why this problem should be solved.

Proposal

Text.Printf.printf and friends report errors in the format string and mismatches between format string and argument types via error. This is the right thing for programmer supplied format strings, but not appropriate for user provided format strings. Think of a server program that wants to write multiple files through a file path pattern like "page%04d.png".

I wish to have a function like

parseFormatString :: (PrintfType f) => String -> Either String f

to be used as in:

do  path <-
        case parseFormatString "page%04d.png" of
            Right prtf -> return prtf
            Left msg -> logAndFail $ "format string parsing error: " ++ msg
    for_ [0..10] $ \k -> writeFile (path (k::Int)) "PNG..."

The programmer provides the type f and thus parseFormatString can not only check for parsing errors in fmt, but it can also check whether the format string is consistent with the programmer provided arguments.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information