2013-06-28 63 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(它们被定义为单字段可变记录)。