English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
[Caml-list] How to read three integers from a text-file... ?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-25 (23:33)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] How to read three integers from a text-file... ?
From: Chris Hecker <checker@d6.com>
> >   Well, this would make a static printf rather that the well known printf
> >   from C, since the format string may be known only at runtime in some
> >   cases.
> As Markus says, the Printf.printf doesn't work in this case anyway right 
> now, since it happens completely at compile time, I believe.

Type-checking is completely at compile time, but you can define your
format independently if you want:

# let fmt : (_,_,_) format = "Hello %s, it's %d:%d.\n";;
val fmt : (string -> int -> int -> 'a, 'b, 'a) format = <abstr>
# Printf.printf fmt "Chris" 9 30;;
Hello Chris, it's 9:30.

Of course this could be done in camlp4 also. The only advantage of the
current approach is that the internal representation of the format is
still a string, which is kind of memory-efficient.

Exercise to the interested reader:
Define a function which, given a string and a concrete representation
of its format type, checks whether the string conforms to the type.
To make it more useful, the concrete representation used should
include the format type in its type, using phantom types.

val check_format : ('a, 'b) format_type -> string -> bool
val make_format : ('a, 'b) format_type -> string -> ('a, 'c, 'b) format

# make_format (arr string (arr int (arr int null))) "Hello %s, it's %d:%d.\n";;
val fmt : (string -> int -> int -> 'a, 'b, 'a) format = <abstr>


Jacques Garrigue
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