Structural Equality in F#

前端 未结 2 1805
情话喂你
情话喂你 2020-12-16 00:47

I have a record type that includes a function:

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

I want this type to have structural equality. Is there some

2条回答
  •  执笔经年
    2020-12-16 01:46

    To answer more specifically your original question, you can create a custom type whose comparison between instances is always true:

    []
    type StructurallyNull<'T> =
        { v: 'T } 
    
        override x.Equals(yobj) =
            match yobj with
            | :? StructurallyNull<'T> -> true
            | _ -> false
    
        override x.GetHashCode() = 0
    

    You can then use it this way:

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

提交回复
热议问题