2016年4月,OCaml 4.03引入了这种语法(“作为数据类型构造函数参数的内联记录”):
http://ocaml.org/releases/4.03.html
有些系统,如我的Ubuntu,包含较旧版本的OCaml,但不支持此功能:
$ /usr/bin/ocaml
OCaml version 4.02.3
# type elem=
|Nil
|Elem of {content:int;mutable next:elem};;
Error: Syntax error
解决此问题的最佳方法是安装较新版本的OCaml并保持更新,最好使用OPAM:
https://opam.ocaml.org/doc/Install.html
如果这不是一个选项,您可以通过声明代数数据类型和记录类型来解决它,同时使用一个相互递归的定义(
type t1 = ... and t2 = ...
):
# type elem = Nil | Elem of elem_record
and elem_record = { content: int; mutable next: elem };;
type elem = Nil | Elem of elem_record
and elem_record = { content : int; mutable next : elem; }