'Can I overload the '=' operator for struct-typed arguments?

I want to get a warning everywhere the equals operator '=' is used on struct-typed arguments. It seems that if I define this operator it does not overload, it just redefines '=' to only work for struct-typed arguments, which is not what I want.

[<Obsolete("Possible performance issue with '=' operator on structs. See https://github.com/dotnet/fsharp/issues/526.")>]
let inline (=) (x:  ^T) (y:  ^T) : bool when   ^T: struct and ^T: (static member (=) :  ^T *  ^T -> bool) =
    x = y

let a = obj()
let x = Guid.NewGuid()
let y = Guid.NewGuid()

a = a |> ignore // oh no - "A generic construct requires that the type 'obj' is a CLI or F# struct type."

x = y |> ignore // ok - gets desired warning

Can this be done in F# as is?

Update: found a possible workaround: simply use the [<NoEquality>] attribute on the affected structs; it does mean that all structs need to be annotated but at least it does help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source