所以这可能是一个愚蠢的问题,但在刚开始使用 OCaml 后,我现在在 utop 中遇到了错误。我试图断言两个整数在结构上不相等。
assert 2 <> 3;;
Error: This expression has type int but an expression was expected of type
bool because it is in the condition of an assertion
Run Code Online (Sandbox Code Playgroud)
整个语句会导致错误,但只需键入我正确断言的表达式即可计算为 true。
2 <> 3;;
- : bool = true
Run Code Online (Sandbox Code Playgroud)
我在原始断言语句中添加了括号,这解决了问题。
assert (2 <> 3);;
- : unit = ()
Run Code Online (Sandbox Code Playgroud)
我只是想知道如果没有括号,最初到底发生了什么导致错误。通常什么时候需要括号?