无法在F#签名文件(.fsi)中使用Literal属性

Tro*_*haw 9 f#

我无法在F#签名文件中定义文字.

我有一个文件,File.fs如下所示:

module Thingo =
  [<Literal>]
  let hello = "world"
Run Code Online (Sandbox Code Playgroud)

我需要为它创建一个签名文件,所以最初尝试this(File.fsi):

module Thingo =
  [<Literal>]
  val hello : string
Run Code Online (Sandbox Code Playgroud)

这导致以下错误File.fsi:A declaration may only be the [<Literal>] attribute if a constant value is also given, e.g. 'val x : int = 1'

我放弃了:

module Thingo =
  [<Literal>]
  val hello : string = "world"
Run Code Online (Sandbox Code Playgroud)

声明的行val hello收到错误:Incomplete structured construct at or before this point in signature file. Expected incomplete structured construct at or before this point or other token.

所以它不喜欢我在签名文件中分配值(或者我不知道如何分配值,非常可能).

该文档说我需要以某种方式分配的签名值:

如果使用该Literal属性,则它必须同时出现在签名和实现中,并且必须为两者使用相同的文字值.

(来自https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/signatures)

我还自动生成了一个签名文件(使用--sigbuild参数)并吐出:

module EventSchema = begin
    [<LiteralAttribute ()>]
        val id : string
end
Run Code Online (Sandbox Code Playgroud)

我尝试了这种确切的语法,但它也没有用.

谢谢阅读!任何建议将不胜感激.

Abe*_*bel 1

正确的答案应该是:这是一个错误。报道如下: https: //github.com/dotnet/fsharp/issues/7897

请注意,作为解决方法,您可以关闭轻语法,然后它将在当前版本中工作。

另请注意,当常量值与签名文件中的值不匹配时,编译器将出错。