I understand that its return type is the string that is read but why does it need the parameter of type unit
?
Every OCaml function takes one value and returns one value1. Since there are no meaningful values that read_line
could take but it must take something, unit
is provided.
And read_line
must be a function, because some work has to be done when it's called.
Likewise, a function which performs an action but for which there is no meaningful return value typically returns unit
in OCaml.
E.g.
# print_endline "Hello, world!";;
Hello, world!
- : unit = ()
1 That one return value may be another function, thus allowing us to create functions which take more than one argument.