caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Aaron Bohannon <bohannon@cis.upenn.edu>
To: caml-list@inria.fr
Subject: Printf and "%a"
Date: Sat, 9 Apr 2005 15:43:54 -0400	[thread overview]
Message-ID: <1daf59ba39e27121b56c47fda1b80d5e@cis.upenn.edu> (raw)

The behavior of the "%a" format string in the Printf module does not 
seem to match the documentation.

# Printf.printf "%a" ;;
- : (out_channel -> '_a -> unit) -> '_a -> unit = <fun>
# Printf.sprintf "%a" ;;
- : (unit -> '_a -> string) -> '_a -> string = <fun>

The first type exactly matches the one that would be expected based 
upon reading the documentation.  The second type surprised me because 
the only documentation for sprintf is:  "Same as Printf.fprintf, but 
instead of printing on an output channel, return a string containing 
the result of formatting the arguments."

On an intuitive level, I can understand that it makes some sense to 
require a user-defined printer that returns a string in the case of 
sprintf.  However, the need for the function to first take a unit 
argument is not intuitive at all to me, and it seems to hinder the 
usefulness of the mechanism because it is not likely that one would 
have already defined a printer function that has that type and can 
simply be plugged in.  So I guess one will generally have to write:

   let s = Printf.sprintf "...%a..." (fun () -> thing_to_string) thing ;;

Then there's kprintf:

# fun k -> Printf.kprintf k "%a" ;;
- : (string -> 'a) -> (unit -> 'b -> string) -> 'b -> 'a = <fun>

It appears to follow the behavior of sprintf, but I read a comment in 
the newsgroup archives that indicated there was more complexity with 
the interaction of kprintf and "%a" format strings.  Does this 
interaction ever change the type of the user-defined printer that is 
expected by kprintf?  Again, the documentation misleadingly silent 
about "%a" patterns used with kprintf.

--
Aaron Bohannon
http://www.cis.upenn.edu/~bohannon/


                 reply	other threads:[~2005-04-09 19:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1daf59ba39e27121b56c47fda1b80d5e@cis.upenn.edu \
    --to=bohannon@cis.upenn.edu \
    --cc=caml-list@inria.fr \
    /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).