open宣言

モジュールの関数を使うときには[モジュール名].[関数名]とするけど,open宣言をすればモジュール名をつけなくても使えるようになる。

 # map (fun x -> x * x) [1;2;3;4];;
 Characters 0-3:
   map (fun x -> x * x) [1;2;3;4];;
   ^^^
 Unbound value map
 # open List;;
 # map (fun x -> x * x) [1;2;3;4];;
 - : int list = [1; 4; 9; 16]

複数のモジュールをopenしたとき,同じ名前の関数がある場合には後からopenしたモジュールの関数だけが使えるようになる。たとえば,map関数は ListモジュールにもArrayモジュールにもあるけど,次のようにするとArrayモジュールのほうだけが使える。

 # open List;;
 # open Array;;
 # map (fun x -> x * 10) [|1;2;3;4|];;
 - : int array = [|10; 20; 30; 40|]

↑Arrayのmapは使える。↓Listのmapは使えない。

 # map (fun x -> x * 10) [1;2;3;4];;
 Characters 22-31:
   map (fun x -> x * 10) [1;2;3;4];;
                         ^^^^^^^^^
 This expression has type 'a list but is here used with type int array

モジュール名をつけてやれば使える。

 # List.map (fun x -> x * 10) [1;2;3;4];;
 - : int list = [10; 20; 30; 40]