Version française
Home     About     Download     Resources     Contact us    
Browse thread
Newbies question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Peter Bruhn <bruhn@i...>
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