2012-12-24 33 views
1

此功能:遞歸函數:這個表達式應該有類型單位。

let rec foo() = 
    try 
    let line = input_line stdin in 
    (try 
     Mparser.tex_expr lexer_token_safe (Lexing.from_string line); 
     print_string ("SUCCESS\n") 
     with 
     Mtexutil.Illegal_tex_function s -> print_string ("$T"^s^" "^line^"\n") 
      | LexerException s   -> print_string ("$L"^line^"\n") 
      | Parsing.Parse_error   -> print_string ("$P"^line^"\n") 
      | _       -> print_string ("$S "^line^"\n")); 
    flush stdout; 
    foo(); 
    with 
    End_of_file ->() 
;; 

給出了錯誤:

Warning 10: this expression should have type unit. 

開始與Mparser.tex行。

如何解決此警告?

回答

4

看來編譯器警告你,Mparser.tex_expr返回一個你不使用的值。你可以清楚地知道你正在有意丟掉價值。這就是ignore功能是什麼:

ignore (Mparser.tex_expr lexer_token_safe (Lexing.from_string line)); 

在某些情況下,我認爲事情讀取let ... in更好,而不是一個分號:

let _ = Mparser.tex_expr lexer_token_safe (Lexing.from_string line) in 
...