Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml Core_unix.fork with Core_unix.exec never returns

Tags:

ocaml

I'm having trouble fork execing in the following manner because the child process returns a Core_kernel.Std.never_returns and the parent is attempting to return ().

I get the error This expression has type unit but an expression was expected of type Core_kernel.Std.never_returns = Core_kernel.Nothing0.t. Can't seem to find the propper way of doing this with 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 *)
like image 679
jpittis Avatar asked Sep 26 '22 14:09

jpittis


1 Answers

The never_returns type is specially designed to be consumed with never_returns function. This is to require a programmer to state clearly in the code, that he understands that the expression doesn't terminate. 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 _ -> ()
like image 64
ivg Avatar answered Oct 11 '22 14:10

ivg