具有相同名称的模块和类

Nik*_*ird 11 f# f#-3.1

这是允许的:

type Test = class end

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test = begin end
Run Code Online (Sandbox Code Playgroud)

但这不是:

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test = begin end

type Test = class end
Run Code Online (Sandbox Code Playgroud)

为什么?

在第二种情况下,错误是:类型或模块"测试"的重复定义.

我希望能够定义一些[<Literal>]类型所需的公共常量,并且对于具有相同名称的模块内的类型的用户来说很重要.

Gus*_*Gus 14

您可以打开类型声明,关闭它并稍后重新打开,如下所示:

type Test = class end

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Test =
    [<Literal>]
    let myLiteral = "myLiteral"

type Test with
    static member test = ()
Run Code Online (Sandbox Code Playgroud)

我一直都在使用这个技巧;)

  • @NikontheThird它看起来像一个类型扩展但它不是.只要它在同一个文件中,F#编译器就会将所有内容放在一起. (3认同)