English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Format.sprintf and "%a"
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-11-23 (12:42)
From: jim.rauser@s...
Subject: Format.sprintf and "%a"

I'm teaching myself Ocaml and have run up against a problem I can't
solve in connection with the format module.  For many of the types
in my code, I have defined pretty-printing functions with type
(Format.formatter -> <type> -> unit), e.g.,

  type foo = Foo | Bar
  let fmt_foo f x =
    match x with
      Foo -> Format.fprintf f "Foo"
    | Bar -> Format.fprintf f "Bar"
  Format.printf "Test: %a@." fmt_foo Foo

Works great, but when I try it with sprintf, I get the following:

  let s = Format.sprintf "Test: %a@." fmt_foo Foo
  This expression has type
    ((Format.formatter -> foo -> unit) -> foo -> unit,
     Format.formatter, unit)
  but is here used with type
    ((Format.formatter -> foo -> unit) -> foo -> unit,
     unit, string)

[Note: I retyped these examples by hand, I hope they're accurate.]

I can see the correlation of the signatures in the error message to
those of fprintf and sprintf in Format.mli, but the type magic that
seems to be occuring in the implementation of the Format modules
is beyond me.  Can someone explain what is going on here?  Is
there a way to use my formatting functions with sprintf?

Thanks, Jim

Jim Rauser                                      mailto:rauser@qcentic.de
Qcentic GmbH
Max-Planck-Str. 39a                                 Tel: 02234/950 36 16
D-50858 Koeln                                        Fax: 02234/27 19 17