F#编译器错误FS0030,值限制问题

时间:2022-07-14 16:10:39

I've read the blurb at StrangeLights, I've read the passage from Expert F# (page 119), but I can't see how they apply to my code:

我已经阅读了StrangeLights的简介,我已经阅读了专家F#(第119页)中的段落,但我看不出它们如何应用于我的代码:

For my tests, I want to check equality between floats, with a bit of tolerance. I'm converting everything to units of measure, but I want to be able to be 'generic':

对于我的测试,我想检查浮点数之间的相等性,并且有一点容忍度。我正在将所有内容转换为度量单位,但我希望能够成为“通用”:

let toleq (e:float<_>) a b = (abs ( a - b ) ) < e

I can then use this to check equality on different 'types' of float, or curry it to make a custom one:

然后,我可以使用它来检查浮点数的不同“类型”上的相等性,或者将其定制为自定义的:

toleqm = toleq 1.0e-10<m>

But I get the following message:

但我收到以下消息:

Type inference has inferred the signature
    val toleq : float<'u> -> float<'u> -> float<'u> -> bool
Either define 'toleq' as a simple data term, make it a function, or add a 
    type constraint to instantiate the type parameters.

I don't see how I can do any more to make it a function - I can't see any implicit parameters.

我不知道如何让它成为一个函数 - 我看不到任何隐含的参数。

What's up?

1 个解决方案

#1


Well, I messed around a bit and found the solution, in desperation, but I'm not sure that I understand why...

嗯,我有点乱了,在绝望中找到了解决方案,但我不确定我理解为什么......

let toleq (e:float<_>) (a:float<_>) (b:float<_>) = (abs ( a - b ) ) < e

Ugh, it's almost as ugly as generic declarations in C#.

呃,它几乎和C#中的泛型声明一样难看。

#1


Well, I messed around a bit and found the solution, in desperation, but I'm not sure that I understand why...

嗯,我有点乱了,在绝望中找到了解决方案,但我不确定我理解为什么......

let toleq (e:float<_>) (a:float<_>) (b:float<_>) = (abs ( a - b ) ) < e

Ugh, it's almost as ugly as generic declarations in C#.

呃,它几乎和C#中的泛型声明一样难看。