Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* Newbies question
@ 1999-10-01 13:01 Peter Bruhn
  1999-10-02 18:04 ` Gerd Stolpmann
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Peter Bruhn @ 1999-10-01 13:01 UTC (permalink / raw)
  To: caml-list

The following function does not compile, since the compiler thinks it has
the wrong return type: 

    let read_res file : ('a * 'b) list =
      let final_result = ref [] in
      let fd = open_in file in
      try 
        let lexbuf = Lexing.from_channel fd in
        while true do
          let result = Parseres.main Lexres.token lexbuf in
          final_result := result :: !final_result 
        done  (* <---- nothing returned here, but we cannot get here anyway
                       the loop is exited by an exception only *) 
      with             
        Lexres.Eof ->  
          close_in fd; !final_result (* !final_result is of type ('a * 'b) list *)
      | Parsing.Parse_error ->          
          close_in fd; printf "ERROR: Ressourcen Datei fehlerhaft\n"; exit (-1);
          
But when I use an empty list after the endless-loop, it works alright. Do I
really have to fake the compiler? Or am I doing something wrong? This works:

    let read_res file : ('a * 'b) list =
      let final_result = ref [] in
      let fd = open_in file in
      try 
        let lexbuf = Lexing.from_channel fd in
        while true do
          let result = Parseres.main Lexres.token lexbuf in
          final_result := result :: !final_result 
        done; [] (* <---------- EMPTY LIST INSERTED HERE *)
      with 
        Lexres.Eof ->
          close_in fd; !final_result
      | Parsing.Parse_error ->
          close_in fd; printf "ERROR: Ressourcen Datei fehlerhaft\n"; exit (-1);
          
Thanks for any comments, hints, flames, ...
Peter
-------------------
bruhn@wu-wien.ac.at




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

end of thread, other threads:[~1999-10-04 17:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-10-01 13:01 Newbies question Peter Bruhn
1999-10-02 18:04 ` Gerd Stolpmann
1999-10-03 10:58 ` Markus Mottl
1999-10-04 13:37 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox