Types OCaml可变变体类型

Types OCaml可变变体类型,types,ocaml,immutability,mutable,mutability,Types,Ocaml,Immutability,Mutable,Mutability,第37页提到可变变体类型: 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

第37页提到可变变体类型:

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建议使用refs,这是可变记录的快捷方式:

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

事实上,可变变量在Caml Light和OCaml之间的转换中被丢弃,部分原因是操作它们的语法非常笨拙(可变字段上的模式匹配将使模式标识符成为左值,yumm…)


当前表示可变性的方法是通过可变记录字段(具有适当的字段突变语法)或引用
int-ref
(定义为单字段可变记录)。

您可以使用refs作为缩写

检查2.2可变存储和来自的副作用