F#编译器错误FS0030,值限制问题 [英] F# compiler error FS0030, problems with the Value Restriction

查看:80
本文介绍了F#编译器错误FS0030,值限制问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 StrangeLights 中阅读了内容简介>,我已经阅读了段落来自Expert F#(第119页),但我看不到它们如何应用于我的代码:

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:

对于我的测试,我想检查一下浮点数之间的相等性,并带有一点宽容度.我正在将所有内容都转换为度量单位,但我希望能够成为通用":

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

然后我可以用它来检查不同类型的float的相等性,或者用它来制作一个自定义的float:

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>

但是我收到以下消息:

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.

怎么了?

推荐答案

好吧,我无所事事,无奈地找到了解决方案,但是我不确定我为什么会这样.

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

嗯,它几乎和C#中的泛型声明一样丑陋.

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

这篇关于F#编译器错误FS0030,值限制问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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