Version française
Home     About     Download     Resources     Contact us    
Browse thread
ocaml 2.02 bug: curried printf
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <Xavier.Leroy@i...>
Subject: Re: another approach to sprintf (Re: ocaml 2.02 bug: curried printf)
> It does, however, mean that people can extend the set of patterns that
> can be used in printf in a more palatable way than the %a mechanism.
> Especially with neat things like Danvy's "lis" combinator.

This is a good point.

> It also allows me to take two formats and concatenate them, like this:
> let foo = int $ lit " " $ int $ string
> let bar = lis int $ lit "!"
> let zum = foo $ bar
> which you can't do with O'Caml's format strings.

You almost can.  The following definition works as long as you
don't have %a and %t escapes in your format strings:

let (^^) (s1 : ('a, 'b, 'c) format) (s2 : ('c, 'b, 'd) format) =
  (Obj.magic (Obj.magic s1 ^ Obj.magic s2) : ('a, 'b, 'd) format)

If you have occurrences of %a or %t in s1, the typing becomes wrong.
This could be fixed by adding a fourth type parameter to the "format"
type constructor, but I agree this is getting really complicated.

- Xavier Leroy