Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Printf question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Printf question
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 = <fun>
> # let myprint n = Printf.sprintf "n=%a" (fun () n->pr n) n;;
> val myprint : int -> string = <fun>
> # 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