2
votes

This bit of code:

open System
open System.Collections.Generic

type INode =
    interface
        inherit IEqualityComparer<INode>
        inherit IEquatable<INode>
    end

let myFun<'N when 'N :> INode> (n:'N) = n = n

generates the following build error:

A type parameter is missing a constraint 'when 'N : equality'   

I know I can appease the compiler at the function level with

let myFun<'N when 'N :> INode and 'N : equality> (n:'N) = n = n

but I'd prefer to let the type constraint as it was in myFun and "fix" the issue at INode, so as to make it easier in case I want to have several functions taking on the very same constraint.

Is it possible?

1

1 Answers

4
votes

You don't need the bit inside the <> which makes the problem go away when it is removed:

open System
open System.Collections.Generic

type INode =
    interface
        inherit IEqualityComparer<INode>
        inherit IEquatable<INode>
    end

let myFun (n:'N when 'N :> INode) = n = n

An even simpler version (from @ben) is to use the shorthand for types which are downcastable as follows:

let myFun (n: #INode) = n = n