Ocaml 表达式需要类型单位,但它已经需要

Ocaml 表达式需要类型单位,但它已经需要,ocaml,try-with,Ocaml,Try With,编译它告诉我,(“hello”;())“应该有类型单位” 事实上,我收到了与此代码相同的警告 let _ = try ("hello"; ()) with | _ -> print_endline "hi" let _ = "hello"; () 还是这个代码 let _ = try ("hello"; ()) with | _ -> print_endline "hi" let _ = "hello"; () 但是它有类型单位。。。不是吗?表

编译它告诉我,
(“hello”;())
“应该有类型单位”

事实上,我收到了与此代码相同的警告

let _ =
    try ("hello"; ()) with
    | _ -> print_endline "hi"
let _ = "hello"; ()
还是这个代码

let _ =
    try ("hello"; ()) with
    | _ -> print_endline "hi"
let _ = "hello"; ()
但是它有类型
单位
。。。不是吗?

表达式:

let _ = ("hello"; ())
 this expression should have type unit - around "hello" string.
触发警告:

 let f  = "hello";1;;
这是因为您试图通过“hello”返回第一个值,然后返回
1
,这意味着ocaml必须忽略“hello”。 如果您将其替换为
单元
——意思是“这里我不返回任何内容”,那么就可以了

表达方式:

let _ = ("hello"; ())
 this expression should have type unit - around "hello" string.
不引发警告,并且
f
int

因此,您得到的警告与表达式的内部代码有关,而与您编写的表达式类型无关

let f = (); 1;;
编译器警告您,您计算的内容在此之后将被忽略(“hello”从未被使用,返回值为
f
is
()
)。但是,正如您所注意到的,
f
具有类型
unit

utop
中:

let f = "hello";();;
你会得到:

let _ = try ("hello"; ()) with
    | _ -> print_endline "hi";;
精确定位到
“hello”
字符串的位置,但不定位到
(“hello”;())
(“hello”;())
具有类型单位,与打印尾行“hi”
完全相同

警告是关于表达式应该是而不是
“hello”应具有类型单位。

表达式:

let _ = ("hello"; ())
 this expression should have type unit - around "hello" string.
触发警告:

 let f  = "hello";1;;
这是因为您试图通过“hello”返回第一个值,然后返回
1
,这意味着ocaml必须忽略“hello”。 如果您将其替换为
单元
——意思是“这里我不返回任何内容”,那么就可以了

表达方式:

let _ = ("hello"; ())
 this expression should have type unit - around "hello" string.
不引发警告,并且
f
int

因此,您得到的警告与表达式的内部代码有关,而与您编写的表达式类型无关

let f = (); 1;;
编译器警告您,您计算的内容在此之后将被忽略(“hello”从未被使用,返回值为
f
is
()
)。但是,正如您所注意到的,
f
具有类型
unit

utop
中:

let f = "hello";();;
你会得到:

let _ = try ("hello"; ()) with
    | _ -> print_endline "hi";;
精确定位到
“hello”
字符串的位置,但不定位到
(“hello”;())
(“hello”;())
具有类型单位,与打印尾行“hi”
完全相同


警告是关于表达式应该是而不是
“hello”
应具有类型单位。

若要明确您的意图并抑制警告,请写入
忽略“hello”
。若要明确您的意图并抑制警告,请写入
忽略“hello”