F# compiler error FS0030, problems with the Value Restriction

雨燕双飞 提交于 2019-12-19 09:37:58

问题


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

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:


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#.



来源:https://stackoverflow.com/questions/416508/f-compiler-error-fs0030-problems-with-the-value-restriction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!