【问题标题】:Type inference with the same constructor使用相同的构造函数进行类型推断
【发布时间】:2020-06-27 07:17:46
【问题描述】:

我正在编写一个小的 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。

【问题讨论】:

    标签: f# type-inference


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 2015-10-31
      • 2019-02-07
      • 1970-01-01
      • 2012-05-12
      • 2011-11-23
      相关资源
      最近更新 更多