我在下面运行一个hello world程序
let x=3;
print_string "hello world!";;
并收到此警告:
File "hello.ml", line 1, characters 6-7:
1 | let x=3;
^
Warning 10 [non-unit-statement]: this expression should have type unit.
我该如何解决?
回想一下您之前问题的答案,
;
是序列运算符,而不是语句终止符。因此,您的代码将被解释为:
let x = (3; print_string "hello world!")
相当于:
let x = print_string "hello world!"
意味着您要丢弃
3
,而 x
将具有 unit
类型,因为这就是 print_string
返回的内容。您可以使用上面的第二种形式“修复它”,即摆脱警告。