2009-06-17 46 views
0

好吧,这让我很生气。这个代码怎么可能不起作用?字典没有找到在使用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)] 

当试图从字典中提取并且不调用任何提供的方法时,代码片段就会死亡。我今天只是疯了,还是有什么不对?

回答

7

您还没有使用p.GetHashCode上的override修饰符(与p.Equals不同)。我的F#-fu缺乏,但这听起来对我来说是一件坏事。它打印出“称为哈希”吗?如果没有,那将是原因,我强烈怀疑...

+0

男人,你是对的。我已经把我的头撞在墙上两个小时了,很多。不能看到,我没有看到 – LDomagala 2009-06-17 11:24:33