【问题标题】:Why does a record in an array cause a type initializer error in heroku?为什么数组中的记录会导致heroku中的类型初始化错误?
【发布时间】:2017-05-29 14:52:54
【问题描述】:

我正在将 F# 应用程序作为脚本部署到 heroku,但遇到了非常奇怪的问题。在下面指出的情况下,我收到以下错误,但在其他情况下没有:

System.TypeLoadException:无法从 程序集'FSI-ASSEMBLY,版本=0.0.0.0,文化=中性, PublicKeyToken=null'。在(包装器托管到本机) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) 在 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr,System.Reflection.Binder 绑定器,System.Object[] 参数,System.Globalization.CultureInfo 文化) 在 :0

错误案例:

type Test = { Test: string }
printfn "%A" [|{Test = "test"}|]  <--- error here

工作案例:

printfn "%A" [|"test"|]

type Test = { Test: string }
printfn "%A" {Test = "test"}

printfn "%A" [{Test = "test"}]

所以看来我不能将记录放入数组中,但我可以将任何内置类型放入数组中。此外,我可以将记录放入列表中。而且记录本身就很好。

为什么记录加数组的组合会报错?

我在这里使用 buildpack: https://github.com/SuaveIO/mono-script-buildpack

使用 mono-4.4.2.11。

Visual Studio 中的本地 fsi 不会发生这种情况。

【问题讨论】:

  • 看起来像一个错误。它只是记录文字,即您可以让绑定记录并将其用于数组元素吗?
  • 另外,如果在数组开始运算符[| 和记录开始运算符{ 之间放置一个空格会怎样?可能是某些东西将三个字符 [|{ 错误地解析为单个标记而不是两个标记。如果是这样,那么编写 [| {...} |] 应该可以解决解析错误。
  • @rmunn 错误只出现在heroku中。

标签: heroku f# typeinitializeexception


【解决方案1】:

这看起来很像this bug in mono

我不确定哪个版本的 mono 受此影响,并且 bug 讨论中的报告有点矛盾 - 但 mono 4.8.0 的最新稳定版本似乎至少适用于一个人......

另一种解决方法是将您的网站更改为使用 fsproj,以便将代码编译并作为可执行文件启动(而不是使用 fsx 文件并通过 F# 交互式运行) - 我认为编译后的代码不会受到这个错误(或其他涉及单反射的潜在错误)。

【讨论】:

猜你喜欢
  • 2020-02-25
  • 1970-01-01
  • 2021-05-31
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-21
相关资源
最近更新 更多