tags:

views:

113

answers:

1

Ok so this is driving me mad. how can this code possibly not work?

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)]

The snippet dies when trying to fetch out of the dictionary and doesnt call any of the provided methods. Am I just crazy today or is there something wrong here?

+7  A: 

You haven't used the override modifier on p.GetHashCode (unlike p.Equals). My F#-fu is lacking, but that sounds like a bad thing to me. Does it print out "called hash"? If not, that would be the reason, I strongly suspect...

Jon Skeet
man you are sooo right. I´ve been banging my head against the wall for two hours now, thx a lot. cant belive i didnt see that
LDomagala
Also, note that your GetHashCode member is actually defined as a property which happens to return a nullary function. If it were a method instead (defined as `p.GetHashCode()` and omitting the `fun () ->` part below), you'd actually get a warning that you're hiding the base implementation rather than overriding it.
kvb