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?

Brian
  • 117,631
  • 17
  • 236
  • 300
Benjol
  • 63,995
  • 54
  • 186
  • 268
  • I cannot reproduce that error message on 1.9.6.2, can you show the whole tiny program that generates it? – Brian Jan 06 '09 at 16:36
  • There is no tiny program, just tried in FSI, that single line gives me the error. – Benjol Jan 07 '09 at 13:11
  • Cross ref to another question on same topic: http://stackoverflow.com/questions/1131456/understanding-f-value-restriction-errors – Benjol Jul 20 '09 at 07:46

1 Answers1

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

Benjol
  • 63,995
  • 54
  • 186
  • 268