I would like to suggest improving the error message and/or language documentation regarding this error.
I had some trouble understanding what was the problem with this function:
let copy_file1 src dest = let fi = open_in src in let fo = open_out dest in (* begin trouble *) let rec loop = let c = input_char fi in output_char fo c; loop in try loop (* end trouble *) with | End_of_file -> close_in fi; close_out fo | e -> close_in fi; close_out fo; raise e
The compiler (ocamlc 4.14) gave me the message
Error: This kind of expression is not allowed as right-hand side of `let rec’
regarding the line
let c = input_char fi, but I could not figure out what kind of expression it was that was not allowed (alternatively, what kind of expressions are allowed). I.e., “This kind” = ???
I was able to work around it by using a
while statement instead of
However, I still wanted to know what was wrong with my
I studied the language documentation for
let rec (1), but did not find anything helpful. It did not seem to say anything about the kind of expressions allowed, or not allowed, to be the right hand side of
Finally I found a couple of discussions here (2, 3) where people had encountered the same problem, and learned that my
loop was not, as I thought, a function, because it had no parameters, and I should add an argument
() to it to make it a function.
Since this error message has baffled me and at least two others , I would suggest either the error message could be improved (e.g., “Did you intend to define a recursive function? It needs at least one parameter.”), or the section 7 documentation on
let rec could contain a warning to this effect, that functions need at least one argument. Or both.
OCaml Manual, The OCaml language, sec. 7 Expressions, OCaml - The OCaml language
Discussion " Why is this “let rec” rejected — but not if I add a
Why is this "let rec" rejected — but not if I add a superfluous parameter?
Discussion “Recursive function”,