James Shapiro
James Shapiro

Reputation: 6186

How does Swift hash Strings?

I'm looking through the Swift implementation of String (https://github.com/apple/swift/blob/master/stdlib/public/core/String.swift)

String implements the Hashable protocol, which means that it must provide a hashValue. However, none is provided in the String implementation linked to above. Where can I find this? I'm assuming String inherits it from somewhere.

Upvotes: 0

Views: 967

Answers (1)

Ahmad F
Ahmad F

Reputation: 31645

Referring to what mentioned to Swift String - Relationships, you are absolutely right about:

String implements the Hashable protocol...

The reason why you cannot see where is the implementation of conforming to Hashable protocol in the link for the file that you provided in the question is:

'String : Hashable' conformance has been moved to a to a separate file. You can find it here: https://github.com/apple/swift/blob/master/stdlib/public/core/StringHashable.swift; Note that the file is StringHashable.swift, but not String.swift.

You can also find the commit of this change here: https://github.com/apple/swift/pull/4612/commits

Upvotes: 3

Related Questions