caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Richard Jones <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Printf question
Date: Sat, 27 Sep 2003 10:14:08 +0100	[thread overview]
Message-ID: <20030927091408.GB14604@redhat.com> (raw)
In-Reply-To: <20030927082007.GA15720@bourg.inria.fr>

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


  reply	other threads:[~2003-09-27  9:14 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-26 18:02 Richard Jones
2003-09-26 19:04 ` Alain.Frisch
2003-09-29  7:44   ` Mike Potanin
2003-09-27  0:11 ` Olivier Andrieu
2003-09-27  7:23   ` Richard Jones
2003-09-27  8:20     ` Basile Starynkevitch
2003-09-27  9:14       ` Richard Jones [this message]
2003-09-27  9:39         ` Maxence Guesdon
2003-09-29 16:42         ` Pierre Weis
2003-09-29 18:13           ` Richard Jones
2003-09-29 19:57             ` Pierre Weis
2003-09-29 21:50               ` Richard Jones
2003-09-29 22:36                 ` Pierre Weis
2003-09-30  8:03                   ` Richard Jones
2003-09-30  8:45                     ` Pierre Weis
2003-09-30  9:17                       ` Michal Moskal
2003-09-30 14:14                         ` Christophe TROESTLER
2003-09-30 13:19                   ` skaller
2003-09-30 20:52                     ` Pierre Weis
2003-10-01 14:39                       ` Christophe TROESTLER
2003-10-01 14:57                         ` Richard Jones
2003-10-01 15:52                           ` [Caml-list] DBI (was: Printf question) Christophe TROESTLER
2003-10-01 16:21                         ` [Caml-list] Printf question Florian Hars
  -- strict thread matches above, loose matches on Subject: below --
2003-05-18  1:34 Brian Hurt
2003-05-18  3:23 ` Manos Renieris
2003-05-18  3:32 ` William Lovas
2003-05-18  6:06 ` Basile STARYNKEVITCH
2003-05-19  9:39   ` Damien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030927091408.GB14604@redhat.com \
    --to=rich@annexia.org \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).