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: Pierre Weis <pierre.weis@i...>
Subject: Re: [Caml-list] Printf question
[...]
> 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 = <fun>

# let sth x =
    prepare "select salary from emp where id = %d and name = %s" x;;
val sth : int -> string -> unit = <fun>

# 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