From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA20094; Wed, 15 Aug 2001 17:27:33 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id RAA19804 for ; Wed, 15 Aug 2001 17:27:32 +0200 (MET DST) Received: from shell5.ba.best.com (shell5.ba.best.com [206.184.139.136]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f7FFRUf26119 for ; Wed, 15 Aug 2001 17:27:31 +0200 (MET DST) Received: from localhost (bpr@localhost) by shell5.ba.best.com (8.9.3/8.9.2/best.sh) with ESMTP id IAA14457; Wed, 15 Aug 2001 08:27:21 -0700 (PDT) Date: Wed, 15 Aug 2001 08:27:21 -0700 (PDT) From: Brian Rogoff To: Johann Spies cc: ocaml mailing list Subject: Re: [Caml-list] Printf.sprintf ? In-Reply-To: <87r8udirem.fsf@bywoner.sun.ac.za> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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 = # 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