2013-06-28 65 views
2

Caml Light manual提到可變的變量類型第37頁:OCaml的可變變量類型

type foo = A of mutable int 
     | B of mutable int * int 

但是這個擴展似乎並沒有被OCaml中的一部分,是這樣嗎? 我是對的嗎?在OCaml中定義可變類型的唯一方法是使用可變記錄或數組?

(* with records *) 
type a = {mutable a: int} 
and b = {mutable b1: int; mutable b2: int} 
and foo = A of a 
     | B of b 

(* with arrays *) 
type foo = A of int array 
     | B of int array 

編輯:感謝@gasche建議使用裁判,這是可變的記錄的快捷方式:

type foo = A of int ref 
     | B of int ref * int ref 

回答

3

事實上,可變的變體,在CAML光與OCaml的之間的過渡下跌,部分因爲操縱它們的語法非常尷尬(在可變字段上進行模式匹配會使模式標識符變成左值,yumm ...)。

當前表達可變性的方式是通過可變記錄字段(具有適當的字段變體語法)或引用int ref(它們被定義爲單字段可變記錄)。