I run a hello world program below
let x=3;
print_string "hello world!";;
and got this warning:
File "hello.ml", line 1, characters 6-7:
1 | let x=3;
^
Warning 10 [non-unit-statement]: this expression should have type unit.
How can I fix it?
>Solution :
Recall from the answers to your previous questions that ; is a sequence operator, not a statement terminator. Your code will therefore be interpreted as:
let x = (3; print_string "hello world!")
which is equivalent to:
let x = print_string "hello world!"
Meaning that you’re throwing away the 3, and x will have type unit because that’s what print_string returns. You can "fix it", i.e. get rid of the warning, by using the second form above.