1+ module example4
2+
3+ // <Snippet1>
4+ open System
5+ open System.Collections .Generic
6+
7+ module PlatformUtils =
8+ let pathEquals a b = String.Equals( a, b, StringComparison.OrdinalIgnoreCase)
9+ let addPath ( hash : byref < HashCode >) path = hash.Add( path, StringComparer.OrdinalIgnoreCase)
10+
11+ [<Struct; CustomEquality; NoComparison>]
12+ type Path ([< ParamArray > ] segments : string []) =
13+ member _.Segments =
14+ Array.AsReadOnly segments
15+
16+ override this.Equals ( obj ) =
17+ match obj with
18+ | :? Path as o -> ( this :> IEquatable<_>) .Equals( o)
19+ | _ -> false
20+
21+ interface IEquatable< Path> with
22+ member this.Equals ( other : Path ) =
23+ Object.ReferenceEquals( this.Segments, other.Segments) ||
24+ not ( isNull this.Segments) &&
25+ not ( isNull other.Segments) &&
26+ this.Segments.Count = other.Segments.Count &&
27+ Seq.forall2 PlatformUtils.pathEquals this.Segments other.Segments
28+
29+ override this.GetHashCode () =
30+ let mutable hash = HashCode()
31+
32+ for i = 0 to this.Segments.Count - 1 do
33+ PlatformUtils.addPath & hash this.Segments[ i]
34+ hash.ToHashCode()
35+
36+
37+ let set =
38+ HashSet< Path> [
39+ Path( " C:" , " tmp" , " file.txt" )
40+ Path( " C:" , " TMP" , " file.txt" )
41+ Path( " C:" , " tmp" , " FILE.TXT" ) ]
42+
43+ printfn $" Item count: {set.Count}."
44+
45+ // The example displays the following output:
46+ // Item count: 1.
47+ // </Snippet1>
0 commit comments