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 SAA02725; Mon, 29 Sep 2003 18:42:49 +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 SAA08977 for ; Mon, 29 Sep 2003 18:42:47 +0200 (MET DST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h8TGgAH12708; Mon, 29 Sep 2003 18:42:10 +0200 (MET DST) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id SAA08681; Mon, 29 Sep 2003 18:42:09 +0200 (MET DST) From: Pierre Weis Message-Id: <200309291642.SAA08681@pauillac.inria.fr> Subject: Re: [Caml-list] Printf question In-Reply-To: <20030927091408.GB14604@redhat.com> from Richard Jones at "Sep 27, 103 10:14:08 am" To: rich@annexia.org (Richard Jones) Date: Mon, 29 Sep 2003 18:42:09 +0200 (MET DST) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Loop: caml-list@inria.fr X-Spam: no; 0.00; pierre:01 weis:01 pierre:01 weis:01 caml-list:01 printf:01 sth:99 dbh:99 salary:01 emp:99 sth:99 type-safety:01 compile-time:01 dbh:99 salary:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk [...] > This is right. I didn't make my original point clear. > > I'd like to write something like: > > let sth = dbh#prepare "select salary from emp where id = %d and name = %s" in > let res = sth#execute 1 "Jones" in > (* ... *) > > The type-safety issue is that the arguments to the #execute method be > checked at compile-time. > > Using %a I need to write something like: > > let sth = dbh#prepare "select salary from emp where id = %a and name = %a" in > let res = sth#execute int_conversion 1 str_conversion "Jones" in > > which is fine but the compiler doesn't check that the id passed is > really an int. I might as well have written: > > let sth = dbh#prepare "select salary from emp where id = ? and name = ?" in > let res = sth#execute [ `Int 1; `String "Jones" ] in > > and just defer the checking to runtime (in fact, defer it to the > database in this case). [...] I'm pretty sure that format string contsnats are typechecked statically with the usual typechecker resolution mechanism (a bit more complex, but no more magic than the typechecking of the -> type constructor). So why not using format values directly ? For instance: # let prepare fmt = Printf.printf fmt;; val prepare : ('a, out_channel, unit) format -> 'a = # let sth x = prepare "select salary from emp where id = %d and name = %s" x;; val sth : int -> string -> unit = # sth 1 "Jones" select salary from emp where id = 1 and name = Jones- : unit = () This is fully statically typechecked as required. Or may be I'm missing something ? Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners