我发现Asttypes和Parsetree都定义了类型常量:
解析树:
type constant =
| Pconst_integer of string * char option
| Pconst_char of char
| Pconst_string of string * string option
| Pconst_float of string * char option
星型:
type constant =
| Const_int of int
| Const_char of char
| Const_string of string * string option
| Const_float of string
| Const_int32 of int32
| Const_int64 of int64
| Const_nativeint of nativeint
并且 Parsetree 将在 ocaml/parsesing/parsetree.mli 中打开模块 Asttypes:
open Asttypes
那么在解析树中定义的常量会覆盖在 Asttypes 中定义的常量吗?
我有这个测试程序:
let ()=
let filename = "/home/wk/prog/LocationTest/c.ml" in
Location.input_name := filename ;
let readhandle = open_in filename in
let buf = Lexing.from_channel readhandle in
Location.init buf filename ;
let ast = Parse.implementation buf in
Printf.printf "%d" buf.lex_buffer_len;
let a=(List.nth ast 0).pstr_desc in
match a with
|Pstr_eval (x,y)->
match x.pexp_desc with
|Pexp_constant z->
match z with
|Pconst_integer (x,y)->
Printf.printf "%d" x;
c.ml 只有一行,定义一个数字
这个程序不能工作,编译器告诉我它需要类型Asttypes.constant。
如果我将最后两行更改为:
|Const_int q->
Printf.printf "%d" q;
它工作正常,并以 c.ml 显示数字
它不会覆盖它,但会隐藏它。因此,编译器仍然知道这两种类型并且仍然存在,但是,当您使用非限定constant
时,它将引用上次打开的模块中定义的类型构造函数。基本上,open
语句只启用非限定访问。您仍然可以访问其他模块的值和类型,前提是使用模块名称限定它们的名称,例如Asttypes.constant
、Parsetree.constant
。构造函数也是如此,例如,Asttypes.Const_int
、值、模块、类和模块中定义的其他项。