我在以以下方式执行fork时遇到麻烦,因为子进程返回a Core_kernel.Std.never_returns,而父进程试图返回()。
我得到了错误This expression has type unit but an expression was expected of type Core_kernel.Std.never_returns = Core_kernel.Nothing0.t。似乎找不到使用的正确方法Core.Std。
open Core.Std
open Unix
let () =
let prog = "ls" in
let args = ["ls"; "-l"] in
match Unix.fork () with
| `In_the_child ->
Unix.exec ~prog:prog ~args:args ();
| `In_the_parent _ ->
(* continue on with the program *)
Run Code Online (Sandbox Code Playgroud)
该never_returns类型是专门为never_returns功能性消费而设计的。这就要求程序员在代码中清楚地说明,他明白表达式不会终止。这是一个工作示例:
let () =
let prog = "ls" in
let args = ["ls"; "-l"] in
match Unix.fork () with
| `In_the_child ->
Unix.exec ~prog ~args () |>
never_returns
| `In_the_parent _ -> ()
Run Code Online (Sandbox Code Playgroud)