具有相同构造函数的类型推断

Type inference with the same constructor

我正在编写一个小的 F# 程序。

type type_a= | T of int | S of string;;
let aa=T 30;;
type type_b= | T of int | S of string;;
let bb =T 40;;

以上,为什么编译器如何区分aa和bb的类型?来自 Visual Studio 的交互式控制台认为 bb 是 type_b 类型,aa 是 type_a.

类型

你们两个被歧视的工会都在暴露T和S。 当调用 let aa=T 30 type_b 未知时,编译器从 type_a 推断 T。 下一行 type_b 被定义,因此来自 type_bTS 现在隐藏 type_aTS。所有对 T 的后续调用都被推断为 type_b.

您仍然可以在其余代码中引用 type_a 中的 T unsing let cc=type_a.T 50;;