Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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-26 (12:17)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] How to read three integers from a text-file... ?
From: Chris Hecker <>
> >   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 Archives:
Bug reports: FAQ:
Beginner's list: