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 LAA09291; Sat, 27 Sep 2003 11:14:12 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id LAA20540 for ; Sat, 27 Sep 2003 11:14:11 +0200 (MET DST) Received: from aomori.annexia.org (annexia.force9.co.uk [212.56.101.183]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id h8R9EA513479 for ; Sat, 27 Sep 2003 11:14:10 +0200 (MET DST) Received: from rich by aomori.annexia.org with local (Exim 3.36 #1 (Debian)) id 1A3B9U-0008WB-00 for ; Sat, 27 Sep 2003 10:14:08 +0100 Date: Sat, 27 Sep 2003 10:14:08 +0100 Cc: caml-list@inria.fr Subject: Re: [Caml-list] Printf question Message-ID: <20030927091408.GB14604@redhat.com> References: <20030926180248.GA14053@redhat.com> <16244.54690.54537.392805@karryall.dnsalias.org> <20030927072325.GA14604@redhat.com> <20030927082007.GA15720@bourg.inria.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20030927082007.GA15720@bourg.inria.fr> User-Agent: Mutt/1.5.4i From: Richard Jones X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 printf:01 basile:01 andrieu:01 val:01 printf:01 sprintf:01 val:01 sth:99 dbh:99 salary:01 emp:99 sth:99 type-safety:01 compile-time:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, Sep 27, 2003 at 10:20:07AM +0200, Basile Starynkevitch wrote: > On Sat, Sep 27, 2003 at 08:23:25AM +0100, Richard Jones wrote: > > On Sat, Sep 27, 2003 at 02:11:14AM +0200, Olivier Andrieu wrote: > > > You could use the %a format. [...] > > > > I guess the problem with this is that I lose any type safety, > > which is the whole point of doing things this way. > > > > I don't think that %a in formats lose type safety (provided that the passed > function has the correct type). > > # let pr= function 0->"zero" | 1->"one" | n -> string_of_int n;; > val pr : int -> string = > # let myprint n = Printf.sprintf "n=%a" (fun () n->pr n) n;; > val myprint : int -> string = > # myprint 1;; > - : string = "n=one" 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). There was a previous posting to this list which discussed this approach, and I'm exploring it as a possible way to wrap database connections for mod_caml - see also: http://caml.inria.fr/archives/200306/msg00218.html and also the response to that message from Christophe Troestler. To be completely safe at compile time, the OCaml code and the database tables would need to be generated from some common source. That approach is probably too heavyweight. Rich. -- Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you. All new technology is irrelevant until it is taken up by the public. ------------------- 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