多相的レコード

多相的な定義はレコードでもできる。次の定義は,既存のデータに「位置情報」を付け加える,というもの。

 # type 'a with_location = {loc_x : float; loc_y : float; body : 'a};;
 type 'a with_location = { loc_x : float; loc_y : float; body : 'a; }

文字に「位置情報」をつけてみると:

 # let c = {loc_x = 50.0; loc_y = 100.0; body = 'X'};;
 val c : char with_location = {loc_x = 50.; loc_y = 100.; body = 'X'}

同様に整数に:

 # let n = {loc_x = 100.0; loc_y = 100.0; body = 100};;
 val n : int with_location = {loc_x = 100.; loc_y = 100.; body = 100}
 

同じ型で文字も整数も扱える。

ただし,具体的な値は別の型(char with_location と int with_location)になるので,一つのリストに入れるようなことはできない。

 # let l = [c; n];;
 Characters 12-13:
   let l = [c; n];;
               ^
 This expression has type int with_location but is here used with type
   char with_location