分号警告

问题描述 投票:0回答:1

我在下面运行一个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.

我该如何解决?

ocaml
1个回答
1
投票

回想一下您之前问题的答案,

;
是序列运算符,而不是语句终止符。因此,您的代码将被解释为:

let x = (3; print_string "hello world!")

相当于:

let x = print_string "hello world!"

意味着您要丢弃

3
,而
x
将具有
unit
类型,因为这就是
print_string
返回的内容。您可以使用上面的第二种形式“修复它”,即摆脱警告。

© www.soinside.com 2019 - 2024. All rights reserved.