4

Here is a specific example. I have a record type with floating point fields and I would like to be able to compare instances of this type for equality but of course the default compiler-generated implementation of structural equality and comparison doesn't account for floating point imprecision, so you can get false positives and negatives in your comparisons.

In my application I have lots of records with many floating point fields each. What would be nice is if I could slap a "digits of precision" attribute on my floating point fields or on the whole record, to tell the compiler to compare floating point values for equality using that many digits of precision. I could override Equals, GetHashCode, and IComparable.CompareTo but it is quite a lot of typing and a heavy maintenance burden.

I guess the other option is a code generator tool to spit out all these overrides for me.

dmg
  • 608
  • 8
  • 15
  • 1
    Did you consider wrapping the floats with a type implementing `[]`, doing this just once, and using the default structural equality and comparison on your records? – kaefer Mar 12 '15 at 10:09
  • Actually that is a very good suggestion, which was suggested to me by my colleague, but these record types are exposed as public surface of the API and I would really prefer to use regular floats so that the records are easier to consume by client code – dmg Mar 12 '15 at 14:37

1 Answers1

2

Checked the F# source code and as far as I can tell this is not supported.

If you want to take a stab at it the magic happens here:

https://github.com/Microsoft/visualfsharp/blob/fsharp4/src/fsharp/augment.fs