On the inferred reality of F# and the mysterious appearance of the equality constraint


Photo by Kees de Vos
(CC license)

I was happily coding away passing functions around and I was adding a function to a list when it appeared:

 f# list does not support the 'equality' constraint because it is a function type

I was a bit puzzled by this. I was quite sure functions where regarded first class citizens in the realm of F#. In fact I had recently added another function to another list without any complaints.

I spent quite a bit of time trying to figure out what caused this. Try googling for answers with the words F#, function, and list in them, not an easy combination when dealing with a functional language. I tried creating simple test cases, but I could not seem to provoke the same error. At some point I started pondering less rational explanations like perhaps my list felt discriminated in some way, or was upset because it was starting to get late.

As it turned out, I passed the list on to a function that used the list in a comparison. The fact that this comparison (list = List.empty) was a poor substitute for the List.isEmpty function i discovered later is another lesson to take from this short tale.

In C# using a list in a comparison would never cause a compile error. Using a list in a standard equality expression would probably not make sense in C#, but let's not bring such minor details into this story. Trying to compare the incommensurable made F# yell at me. Had I not been so suprised by the unexpexted fault I would have seen that in addition to telling me off, F# was also telling me why. At the end of the error message there was a reference to the exact postion in my code were my equality statement was.