> On Jul 6, 2016, at 9:54 AM, Alexey Komnin via swift-users 
> <swift-users@swift.org> wrote:
> 
> Here is the code:
> 
>    let a: String = "abc"
>    let b: NSString = "abc"
> 
>    assert(a == b)
>    assert(a.hashValue == b.hashValue, "a.hashValue(\(a.hashValue)) !=
> b.hashValue(\(b.hashValue))")

There's no problem if you use generics to select a single Hashable 
implementation:

        import Foundation
        
        func assertHashableConsistent<T: Hashable>(a: T, b: T) {
            assert(a == b, "a and b are equal")
            assert(a.hashValue == b.hashValue, "a and b have the same hash 
value")
        }
        
        assertHashableConsistent(a: "abc" as String, b: "abc" as NSString)

The problem is that, in your case, `a` uses `NSString`'s `Hashable` in the 
first line, but `String`'s `Hashable` in the second line. The 
`assertHashableConsistent(a:b:)` function, on the other hand, ensures that `a` 
uses `NSString`'s `Hashable` in both lines.

-- 
Brent Royal-Gordon
Architechies

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to