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
AW: 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-25 (15:44)
From: jim.rauser@s...
Subject: AW: Format.sprintf and "%a"
Pierre Weis wrote:
> Nothing more than the reported type error: your functions  are
> designed to return unit (they work by side effect) not string (which
> would mean they are functional), and this is obviously uncompatible.
> let s =
>  Format.fprintf str_formatter "Test: %a@." fmt_foo Foo;
>  Format.flush_str_formatter ();;
> val s : string = "Test: Foo\n"

Okay, I found the definition of the type "format" in pervasives.mli,
so I understand the type error now.  But I then tried to partially
apply Format.fprintf to Format.str_formatter:

  let fsprintf = Format.fprintf Format.str_formatter;;
  val fsprintf : ('_a, Format.formatter, unit) format -> '_a = <fun>

Looks good, except for the ominous "_" in front of the type variable
(what does it mean, anyway?).  But:

  let fmt_string f s = Format.fprintf f "%s" s;;
  fsprintf "%a@." fmt_string "foo";;
  This expression has type Format.formatter -> string -> unit
  but is here used with type (string -> 'a, Format.formatter, unit) format

This type error also makes sense.  What I don't understand is,
why it *does* work with Format.fprintf, that is, how the compiler
convinces itself that an argument list like ["%a" fmt_string "foo"]
unifies with the type [('a, Format.formatter, unit) format]?  I don't
recall seeing anything else in the manual that talks about functions
with variable arity; is there some extra-linguistic magic going on here?

Thanks again,


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