F# compiler error FS0030, problems with the Value Restriction
Asked Answered
C

1

3

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?

Chemush answered 6/1, 2009 at 13:26 Comment(3)
I cannot reproduce that error message on 1.9.6.2, can you show the whole tiny program that generates it?Pelagia
There is no tiny program, just tried in FSI, that single line gives me the error.Chemush
Cross ref to another question on same topic: #1131956Chemush
C
2

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

Chemush answered 6/1, 2009 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.