2016-01-22 176 views
4

我遇到了以下列方式fork fork失敗的問題,因爲子進程返回Core_kernel.Std.never_returns,並且父級正試圖返回()OCaml Core_unix.fork與Core_unix.exec永遠不會返回

我收到錯誤This expression has type unit but an expression was expected of type Core_kernel.Std.never_returns = Core_kernel.Nothing0.t。似乎無法找到與Core.Std這樣做的propper方式。

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 *) 

回答

3

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 _ ->() 
相關問題