On Tuesday, May 14, 2002, at 06:37 AM, anatoli wrote:

> Brian Huffman <[EMAIL PROTECTED]> wrote:
>> Here is a printf-style function that I hacked up this morning; it uses 
>> type
>> classes but it doesn't need functional dependencies:
> [snip]
>
> It's very nice and even extendable, though `class Printf String'
> is unfortunately not Haskell 98.

I agree that it is a very nice use of type classes.  But all type 
checking
is done at runtime, because the code which is generated depends not
on the string itself, but on the types of the arguments which are applied
to (printf <<format string>>).
For example,
        putStrLn $ printf "%s" (1 :: Integer)
gives no error at compilation, but fails at runtime with:
        Program error: printf: extra integer argument

> But the bigger question is, how  to support Posix-style positional
> arguments? They are essential for i18n.

I hacked Brian's code to add this feature, see the attachment.

Attachment: Printf.hs
Description: Binary data



> For instance,
>
>> printf "%1$s %2$s" "foo" "bar" -- ==> "foo bar"
>> printf "%2$s %1$s" "foo" "bar" -- ==> "bar foo"
>
> Naturally, such format strings cannot be pre-processed by the
> compiler since they are typically loaded from some message
> database at run time.

Then you give up static type checking for format strings...
Why not let the compiler pre-process this database, and generate
some type-safe dynamically loadable object ?
Or, you could embed a very restricted version of the compiler in
the program, to pre-process and type-check the format strings at runtime
(Yes, you would need to keep some type information in the executable
program).

--
Sébastien

Reply via email to