Newbies question

From: Peter Bruhn (bruhn@isis.wu-wien.ac.at)
Date: Fri Oct 01 1999 - 15:01:28 MET DST


Date: Fri, 1 Oct 1999 15:01:28 +0200 (CEST)
From: Peter Bruhn <bruhn@isis.wu-wien.ac.at>
To: caml-list@inria.fr
Subject: Newbies question

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



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:25 MET