我无法理解来自ocaml编译器的ocaml代码源代码:
File: d:srcocaml-4.07.0driverpparse.ml
50: type 'a ast_kind =
51: | Structure : Parsetree.structure ast_kind
52: | Signature : Parsetree.signature ast_kind
有定义类型astkind,定义类型参数'a,但不使用它?
我知道类型定义的常见用法如下:
type a=
|A of int
|B of int
所以
Structure : Parsetree.structure ast_kind
意思是什么?Structure的类型是Parsetree.Structure?还是ast_kind?
我阅读了官方文档:http://caml.inria.fr/pub/docs/manual-ocaml-312/manual016.html#@manual.kwd53
它告诉我只有在定义记录时才能使用":">
type-representation ::= = constr-decl { | constr-decl }
∣ = { field-decl { ; field-decl } }
field-decl ::= field-name : poly-typexpr
∣ mutable field-name : poly-typexpr
那么这个代码段的含义是什么呢?谢谢
从:开始
50: type 'a ast_kind =
51: | Structure : Parsetree.structure ast_kind
52: | Signature : Parsetree.signature ast_kind
如下所示:
第50行:我们定义了一个参数化类型CCD_ 1,其参数为CCD_。该参数稍后在第51&52第51行:'a
参数类型为Parsetree.structure
第52行也是如此。
现在,更一般地说,ast_kind
是GADT类型(广义代数数据类型(,请参阅GADT手册和另一个示例:Mads-hartmann。
请注意,GADT已在Ocaml 4.00中引入,因此您引用的关于文档的链接对于该特定功能来说已经过时,因为它指的是Ocaml 3.12。您目前正在检查Ocaml 4.07的源代码。