编辑:从目前为止添加的答案和评论看来,我没有正确解释我想要的内容.这是一个例子:
// type not supporting any type of comparison
[<NoEquality>]
[<NoComparison>]
type blah () =
    member x.huha = 0
// make a map, turns out to work whether x supports equality or not
let inline tt x =
    Map.ofList [1, x]       
let test () =
    // maps can be compared for equality if the argument can
    if (tt 1 = tt 2) then failwithf "strange"
    // maps can be made no matter if the argument supports equality
    if (tt (blah ())).Count <> 1 then failwithf "size"
    // this does not compile
    if tt (blah ()) = tt (blah ()) then ....
简而言之,我希望我自己的类型就像上面的地图一样.因此它应该在类型参数执行时支持相等,而不应该在类型参数不支持时执行.我也希望typechecker在不支持时阻止我使用相等,因为它显然可以为内置类型做到这一点.再次感谢.
原始问题:当且仅当某些基础类型具有时,各种内置F#类型才支持相等性.例如,Map<'k, 'd>将支持iff的相等性'd(并且这在编译时检测到).是否可以在用户代码中实现此行为?这是一个失败的尝试,如果相等是无条件的,则编译好的版本.非常感谢.
[<NoComparison>]
type test_fails<[<EqualityConditionalOn>]'a> (content:'a) =
    let eq_impl (x:test_fails<'a>) (y:obj) =
        let y = y :?> test_fails<'a>
        x.content = y.content
    member x.content = content
    override x.Equals (y:obj) =
        eq_impl x y
[<NoComparison>]
type test_compiles<'a when 'a : equality> (content:'a) =
    let eq_impl (x:test_compiles<'a>) (y:obj) =
        let y = y :?> test_compiles<'a>
        x.content = y.content
    member x.content = content
    override x.Equals (y:obj) =
        eq_impl x y
您已经拥有解决方案的一部分:使用[<EqualityConditionalOn>]泛型参数.
您缺少的部分:您需要在地图实现中使用Unchecked.equals而不是常规=运算符(在您检查两个'a值的相等性的任何位置).Unchecked.equals在运行时检查该类型是否支持泛型相等.如果是,则像往常一样比较两个实例/值的相等性; 如果没有,它将回退到结构相等性检查或类型的Object.Equals(obj)方法实现.