Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OCaml open only certain values/types from module

Tags:

ocaml

Does OCaml have an equivalent (possibly involving a camlp4 directive) of

from module import value1, value2 in Python or

use Module qw[value1 value2]; in Perl ?

I'd like to be able to write something like

open Ctypes (@->), string;; or open Ctypes ((@->), string);;

instead of

let (@->) = Ctypes.(@->);;
let string = Ctypes.string;;
like image 220
Gregory Nisbet Avatar asked Aug 13 '16 22:08

Gregory Nisbet


2 Answers

The closest is:

let value1, value2 = Module.(value1, value2) 

For this very reason, open statements are most of the time evil (especially at the top level).

like image 98
mookid Avatar answered Sep 23 '22 01:09

mookid


I'm afraid there is no such partial module opening like what you described.

  • If your modules have no naming conflict, then it's simple enough to just use open Ctypes

  • Otherwise, you can shorten the module name by

    module CT = Ctypes
    
    (* and then just call CT.some_function instead of Ctypes.some_function *)
    
  • Your solutions is quite a neat one, but be careful that the name string may override the primitive type string of OCaml.

    let (@->) = Ctypes.(@->);;
    let string = Ctypes.string;;  (* override the type string *)
    
like image 33
Trung Ta Avatar answered Sep 26 '22 01:09

Trung Ta