F#中的结构平等

时间:2012-06-09 00:47:51

标签: f# structural-equality

我有一个包含函数的记录类型:

{foo : int; bar : int -> int}

我希望这种类型具有结构相等性。有没有什么方法可以标记在等式测试中应该忽略bar?或者还有其他方法吗?

2 个答案:

答案 0 :(得分:17)

请参阅Don关于此主题的blog帖子,特别是自定义平等和比较部分。

他给出的例子几乎与你提出的记录结构相同:

/// A type abbreviation indicating we’re using integers for unique stamps on objects
type stamp = int

/// A type containing a function that can’t be compared for equality  
 [<CustomEquality; CustomComparison>]
type MyThing =
    { Stamp: stamp;
      Behaviour: (int -> int) } 

    override x.Equals(yobj) =
        match yobj with
        | :? MyThing as y -> (x.Stamp = y.Stamp)
        | _ -> false

    override x.GetHashCode() = hash x.Stamp
    interface System.IComparable with
      member x.CompareTo yobj =
          match yobj with
          | :? MyThing as y -> compare x.Stamp y.Stamp
          | _ -> invalidArg "yobj" "cannot compare values of different types"

答案 1 :(得分:9)

要更具体地回答原始问题,您可以创建一个自定义类型,其实例之间的比较始终为真:

[<CustomEquality; NoComparison>]
type StructurallyNull<'T> =
    { v: 'T } 

    override x.Equals(yobj) =
        match yobj with
        | :? StructurallyNull<'T> as y -> true
        | _ -> false

    override x.GetHashCode() = 0

type MyType = { 
    foo: int; 
    bar: StructurallyNull<int -> int> 
}