【问题标题】:F# Implicit ModuleSuffix not working when using fully qualified nameF# Implicit ModuleSuffix 在使用完全限定名称时不起作用
【发布时间】:2020-01-30 03:14:11
【问题描述】:

我有一个 F# 项目,其中包含一个定义类型和模块的文件,如下所示:

module Domain 
  type TodoID = TodoID of int
  module TodoID = let value (TodoID i) = i

  let ti = TodoID 1
  let i1 = TodoID.value ti // Works as expected

F# 的编译器会自动将后缀“Module”添加到 TodoID,因此解包函数的完全限定名称为 Domain.TodoIDModule.value,正如预期的那样(参见 FS-1019)。

当我从另一个文件中引用该函数时,打开Domain模块如下,一切都很好:

module DTO_working
  open Domain

  let ti = TodoID 1
  let i1 = TodoID.value ti // Happiness

但是,当我有一个没有打开模块的文件,而是尝试完全限定函数时,我无法让它工作:

module DTO_broken
  let ti = Domain.TodoID 1
  let i1 = Domain.TodoID.value ti // error FS0039: The field, constructor or member 'value' is not defined.
  let i2 = Domain.TodoIDModule.value ti // error FS0039: The value, constructor, namespace or type 'TodoIDModule' is not defined

当我在 Visual Studio 中将鼠标悬停在 Domain.TodoID.valuevalue 部分时,它会正确显示全名为 Domain.TodoIDModule.value

当我手动添加Module 后缀并在任何地方输入它时,它可以工作。

我错过了什么?是否可以在不打开模块的情况下引用具有自动“模块”后缀的函数?

提前感谢您的帮助,

罗兰

【问题讨论】:

  • 嗯,当您使用模块名称前缀时,它看起来像,例如Domain.TodoID,它总是指 DU case TodoID,而不是类型 TodoID,而不是根据上下文选择一个。在我看来这有点奇怪,可能是 F# 中的一个错误。

标签: compiler-errors f#


【解决方案1】:

首先,让我先说 - 这应该可行。

我从F# tests on auto module suffix开始:

module Ok1

module A =
    let create() = 1
    type Dummy = A | B


type A = 
    member x.P = 1

let works = (typeof<A.Dummy>.FullName.Contains("AModule"))

这很好用。似乎是当一个 DU 案例同名时。

但是有a test case that covers this:

module Ok13

type A = A of string

module A = 
    let create() = 1
    type Dummy = A | B

但在另一个文件中,

module Ok13Test

let i1 = Ok13.A.create ()

确实失败了。 将TodoIdDU案例重构为TodoIDCase也没有问题:

type TodoID = TodoIDCase of int

module TodoID =
    let public value (TodoIDCase i) = i

问题不在于生成模块后缀,它正确地执行了(IL 验证),而是类型推断为 DU 案例分配了更高的可见性。如果你降低能见度,

type private A = A of string

它又开始工作了。这似乎是类型推断的问题,因此现在最好重构 DU 案例并提交错误报告。

【讨论】:

猜你喜欢
  • 2016-04-21
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 1970-01-01
  • 2021-10-13
  • 2018-08-11
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多