caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Checking for eof
@ 2004-12-26  1:12 romildo
  2004-12-26  1:58 ` [Caml-list] " briand
  0 siblings, 1 reply; 6+ messages in thread
From: romildo @ 2004-12-26  1:12 UTC (permalink / raw)
  To: caml-list

Hi.

I am missing a way of checking end of file
for an input channel. How would I write
it?

  val eof : in_channel -> bool

Romildo


^ permalink raw reply	[flat|nested] 6+ messages in thread
* RE: [Caml-list] Checking for eof
@ 2004-12-26 15:25 Don Syme
  2004-12-27  0:14 ` skaller
  0 siblings, 1 reply; 6+ messages in thread
From: Don Syme @ 2004-12-26 15:25 UTC (permalink / raw)
  To: Nicolas George, Caml mailing list; +Cc: Andrew Kennedy, Nick Benton


Nick Benton and Andrew Kennedy have addressed this rather under-appreciated deficiency in ML exceptions in their paper "Exceptional Syntax"

http://research.microsoft.com/~akenn/sml/ExceptionalSyntax.pdf

The topic has also been raised here before http://caml.inria.fr/archives/200407/msg00028.html 

I think this is so sensible that it should be adopted in all variants of ML. 

An OCaml or F# version of their construct might be 
  "let try <bindings> 
   in <expr> 
   with <matching>"

Only the bindings are covered by the "try".  e.g.

let readfile chan =
  let rec loop rlst =
    let try line = input_line chan
        in loop (line :: rlst)
        with End_of_file -> List.rev rlst
  in
    loop []

(note: reduces 17 lines to 7)

Another possibility might be 

  "let try <bindings> 
   with <matching>
   in <expr> "

or indeed you could support both of the above, leaving it up to the programmer to choose where to place the ever-awkward handling code. Unfortunately the syntax 

  "try let <bindings> 
   in <expr> 
   with <matching>"

is too ambiguous when iterated "let ... in" bindings are used.

Don


-----Original Message-----
From: caml-list-admin@yquem.inria.fr [mailto:caml-list-admin@yquem.inria.fr] On Behalf Of Nicolas George
Sent: 26 December 2004 14:09
To: Caml mailing list
Subject: Re: [Caml-list] Checking for eof

Le sextidi 6 nivôse, an CCXIII, briand@aracnet.com a écrit :
>       try
>         (input_line chan), false
>       with
>         | End_of_file -> "", true

I would have written that

	try
	  Some (input_line chan)
	with
	  | End_of_file -> None

but the idea is the same. I find it is an irritating limitation of OCaml
syntax to have to pack and then unpack all local values in order to uncatch
exceptions. Something like

	try
	  let line = input_line chan in
	  untry
          loop (line :: rlst)
	with
	  | End_of_file -> List.rev rlst

This syntax is somewhat awkward: untry is neither a third member of the
try...with structure, because it must be inside the flow of let...in
declaration, nor a stand-alone statement, because it must not be allowed
anywhere outside try...with.

On the contrary, as far as I can see, the semantics is quite simple.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-12-27 20:23 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-26  1:12 Checking for eof romildo
2004-12-26  1:58 ` [Caml-list] " briand
2004-12-26 13:09   ` Nicolas George
2004-12-27 20:23     ` Martin Jambon
2004-12-26 15:25 Don Syme
2004-12-27  0:14 ` skaller

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).