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] Resource acquisition is initialization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] Resource acquisition is initialization
> Maybe I don't understand something but IMHO it's not very usable because
> with_file_in type is fixed after the first use. So e.g. input_line and
> input_char operation cannot be mixed. You cannot write something like
> let process filename =
>   let f = with_file_in filename in
>      begin
>        print_string (f input_line);
>        print_char   (f input_char); (* syntax error! *)
>      end

Even without the typing problem, this would open and close the file
twice, which isn't what you want.  The correct usage is:

let process filename =
  with_file_in filename
    (fun ic ->
       print_string (input_line ic);
       print_char (input_char ic))

- Xavier Leroy
To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: