字典找不到使用F#时放入的键

LDo*_*ala 0 .net f#

好的,这让我很生气.这段代码怎么可能不起作用?

open System.Collections.Generic
type testing (a:int)=
    let test = [Empty]
    member p.GetHashCode =
        fun () -> printfn "called hash";0

    override p.Equals(other:obj) =
        printfn "called equals"
        true
    interface IEquatable<testing> with
        override p.Equals(other:testing) =
            true
    static member op_Equality (other:obj) = printfn "called op" ;true   
let dict2 = new Dictionary<testing,int>()
dict2.[(testing 50)] <- 50
dict2.[(testing 50)]
Run Code Online (Sandbox Code Playgroud)

当试图从字典中取出并且不调用任何提供的方法时,代码段就会死掉.我今天只是疯了还是这里有什么问题?

Jon*_*eet 7

你还没有使用过override修饰符p.GetHashCode(不像p.Equals).我的F#-fu缺乏,但这对我来说听起来不错.它打印出"被叫散列"吗?如果没有,那就是原因,我强烈怀疑......

  • 另请注意,您的GetHashCode成员实际上被定义为恰好返回一个nullary函数的属性.如果它不是一个方法(定义为'p.GetHashCode()`和省略`乐趣() - >`以下部分),你实际上得到的是你隐藏的基本实现,而不是覆盖它警告. (4认同)