Skip to content

HashIdentity.Structural is a bad choice for Dictionary comparer #574

@manofstick

Description

@manofstick

dict, groupBy & dict all use HashIdentity.Structural for the equality comparer, but it is not a good choice as it only supports partial equality relationship. i.e.

let nans = [| nan; nan |]

let dict1 = System.Collections.Generic.Dictionary System.Collections.Generic.EqualityComparer.Default
nans |> Seq.iter (fun n -> dict1.[n] <- n)

let dict2 = System.Collections.Generic.Dictionary HashIdentity.Structural
nans |> Seq.iter (fun n -> dict2.[n] <- n)

printfn "dict1.Count=%d" dict1.Count
printfn "dict2.Count=%d" dict2.Count
printfn "dict1.ContainsKey nan=%b" (dict1.ContainsKey nan)
printfn "dict2.ContainsKey nan=%b" (dict2.ContainsKey nan)

Which outputs the following

dict1.Count=1
dict2.Count=2
dict1.ContainsKey nan=true
dict2.ContainsKey nan=false

Now I'm not recommending the use of System.Collections.Generic.EqualityComparer.Default as this is obviously deficient in other ways, but rather an ER compatible version.

This would be a breaking change, although I think the current version is confusing, as I think most users would think it would act in the same way as the Default EqualityComparer, and most people would be using it in that fashion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions