OCaml Core_unix.fork with Core_unix.exec never returns

I had a problem handling fork as follows, because the child process is returning Core_kernel.Std.never_returns, and the parent is trying to return ().

I get an error This expression has type unit but an expression was expected of type Core_kernel.Std.never_returns = Core_kernel.Nothing0.t. He seems unable to find a way to use this with help 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 *)
+4
source share
1 answer

Type never_returnsspecially designed for use with function never_returns. This requires the programmer to clearly indicate in the code that he understands that the expression does not end. Here is a working example:

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 _ -> ()
+3
source

Source: https://habr.com/ru/post/1625513/


All Articles