caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Rogoff <bpr@best.com>
To: Johann Spies <jspies@maties.sun.ac.za>
Cc: ocaml mailing list <caml-list@inria.fr>
Subject: Re: [Caml-list] Printf.sprintf ?
Date: Wed, 15 Aug 2001 08:27:21 -0700 (PDT)	[thread overview]
Message-ID: <Pine.BSF.4.21.0108150810370.27897-100000@shell5.ba.best.com> (raw)
In-Reply-To: <87r8udirem.fsf@bywoner.sun.ac.za>

On 15 Aug 2001, Johann Spies wrote:

> Can somebody please explain this to me:
> 
> This works as expected:
> 
> # let s = Printf.sprintf "%s" "enigiets";;
> val s : string = "enigiets"
> 
> But this not:
> 
> # let q = "%s";;
> val q : string = "%s"
> # let s = Printf.sprintf q "enigiets";;
> Characters 23-24:
> This expression has type string but is here used with type
>   (string -> 'a, unit, string) format
> 
> Why?

Because Printf is a bit of a hack, in that the syntax for a format string
is the same as the syntax for a regular string. As a beginner, when you
use Printf, it's easier to use the format string as a literal and not
as a variable. If you feel you must do this, the polite way is as follows 

# let q = ("%s" : ('a, 'b, 'c) format);;  
val q : (string -> 'a, 'b, 'a) format = <abstr>
# let s = Printf.sprintf q "enigiets";;
val s : string = "enigiets"

and there's also a rude way (forgive me Berke! :) 

  let s : string = Printf.sprintf (Obj.magic q) "enigiets";;

-- Brian

(*) Yes, I'm a C programmer too so it's a welcome hack. 

-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


  reply	other threads:[~2001-08-15 15:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-08-15  9:49 Johann Spies
2001-08-15 15:27 ` Brian Rogoff [this message]
2001-08-16  9:27   ` Remi VANICAT
     [not found] <200108151045.f7FAjl423204@sarg.ryerson.ca>
2001-08-15 11:04 ` Johann Spies

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=Pine.BSF.4.21.0108150810370.27897-100000@shell5.ba.best.com \
    --to=bpr@best.com \
    --cc=caml-list@inria.fr \
    --cc=jspies@maties.sun.ac.za \
    /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).