Parsetree 中定义的常量会覆盖 Asttypes 中定义的常量吗?

Will constant defined in Parsetree will override the constant defined in Asttypes?

我发现 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 / parsing / parsetree.mli 中打开模块 Asttypes :

open Asttypes

所以Parsetree中定义的常量会覆盖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.constantParsetree.constant。构造函数也是如此,例如 Asttypes.Const_int、值、模块、类 和模块中定义的其他项目。