caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <weis@pauillac.inria.fr>
To: jim.rauser@sdm.de
Cc: caml-list@pauillac.inria.fr
Subject: Re: Format.sprintf and "%a"
Date: Thu, 23 Nov 2000 13:42:04 +0100 (MET)	[thread overview]
Message-ID: <200011231242.NAA30735@pauillac.inria.fr> (raw)
In-Reply-To: <C9A98ED35114D31197D000805FEA668E02650F9F@mucexch.muc.sdm.de> from "jim.rauser@sdm.de" at "Nov 23, 100 12:54:18 pm"

> Hello,
> 
> 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"
>   in
>   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)
>     format
>   but is here used with type
>     ((Format.formatter -> foo -> unit) -> foo -> unit,
>      unit, string)
>     format
> 
> [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?

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.

>  Is there a way to use my formatting functions with sprintf?

Yes. Use the special Format.str_formatter predefined formatter. It
works as usual side effecting formatters (hence it is compatible wih
your fmt_* functions), except that it records the output into a string
buffer. Hence, you would have to flush str_formatter explicitely, to
get the corresponding string result. For instance:

let s =
 Format.fprintf str_formatter "Test: %a@." fmt_foo Foo;
 Format.flush_str_formatter ();;
val s : string = "Test: Foo\n"

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/




      reply	other threads:[~2000-11-23 12:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-23 11:54 jim.rauser
2000-11-23 12:42 ` Pierre Weis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200011231242.NAA30735@pauillac.inria.fr \
    --to=weis@pauillac.inria.fr \
    --cc=caml-list@pauillac.inria.fr \
    --cc=jim.rauser@sdm.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).