当使用独立开关编译并从另一个项目引用时,F#函数会更改类型 [英] F# function changes type when compiled with standalone switch and referenced from another project

查看:69
本文介绍了当使用独立开关编译并从另一个项目引用时,F#函数会更改类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio项目的F#库中,我已将函数定义为

In a Visual Studio project for an F# library I have defined a function as

let inline Estimate (s : ^a seq) (f : float) (w : int) : float * float = ..

Estimate的类型是

val Estimate : s:seq<'a> -> f:float -> w:int -> float*float

从该项目内的脚本中调用Estimate可以正常工作.

Calling Estimate from a script within that project works as expected.

现在,如果我使用--standalone开关编译项目并引用另一个项目的输出DLL,则显示Estimate

Now if I compile the project with the --standalone switch and reference the output DLL from another project, Estimate is shown to be

Estimate<'a,'a>(s: Collections.Generic.IEnumerabls<'a>, f: float, w:int) : float*float

即现在有一些原因需要元组参数. 因此以下内容不起作用

i.e. it some reason now takes tuple arguments. Thus the following does not work

let q, p = EstimationQuality.Estimate x f 1 // This value is not a function and cannot be applied

但是使用元组参数调用它会很好

but calling it with tuple parameters works fine

let q, p = EstimationQuality.Estimate (x, f, 1) // No problem.

这是怎么了?这是编译器中的错误吗?

What's wrong here? Is it a bug in the compiler?


深入研究,看来问题与LanguagePrimitives.GenericZero的使用有关.


Digging a little deeper, it appears that the problem is linked with the use of LanguagePrimitives.GenericZero.

虽然实际上是通过tuple参数调用来编译问题,但是在调用Estimate时却遇到了运行时错误.

While the problem actually compiles with the tuple parameter call, i get a runtime error when Estimate is called.

类型为'System.TypeInitializationException'的未处理异常 发生在LibraryTest.dll

An unhandled exception of type 'System.TypeInitializationException' occurred in LibraryTest.dll

其他信息:的类型初始值设定项 "GenericZeroDynamicImplTable`1"引发了异常.

Additional information: The type initializer for 'GenericZeroDynamicImplTable`1' threw an exception.

推荐答案

使用独立开关编译要从F#中使用的F#DLL并不是一个好主意.

Compiling an F# DLL which is intended to be used from F#, with the standalone switch is not a good idea.

为什么?因为所有F#元数据都丢失了,因为DLL中包含了整个F#类型集,所以这些类型与调用DLL或fsi的F#应用程序的类型具有不同的标识.

Why? Because all the F# metadata is lost since the whole set of F# types are included in your DLL so those types get a different identity from the types of the F# application that call your DLL or fsi.

调用程序程序集使用Fsharp.Core.dll中的类型,这些类型现在与独立编译的DLL中使用的类型不同.

The caller assembly uses the types in Fsharp.Core.dll which now are not the same as the ones used in your standalone compiled DLL.

这就是为什么您看到元组参数的原因,就像从C#看到的那样,它根本不了解F#元数据.

That's why you see the tupled arguments, as seen from C# which doesn't understand F# metadata at all.

使用静态约束的通用内联函数也会中断,因为它们需要元数据在调用站点内联.

Generic inline functions using static constraints break as well since they need the metadata to inline at the call site.

将调用程序程序集也编译为独立程序会使情况变得更糟,那么您将拥有3套具有不同标识的Fsharp类型.

Compiling also the caller assembly as standalone would make things worse, then you will have 3 sets of Fsharp types with different identities.

我认为独立开关仅在最终用户"应用程序中使用时是可以的.

I think the standalone switch is fine when used only in the 'end-user' application.

这篇关于当使用独立开关编译并从另一个项目引用时,F#函数会更改类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆