[swift-users] Equality is broken, isn't it?

Dmitri Gribenko gribozavr at gmail.com
Wed Jul 6 11:59:56 CDT 2016


On Wed, Jul 6, 2016 at 9:54 AM, Alexey Komnin via swift-users
<swift-users at 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))")
>
> It fails with error:
>     assertion failed: a.hashValue(4799450059707601744) != b.hashValue(516202353)
>
> Perhaps, there is an issue with the equality operator.

String and NSString use different comparison algorithms, and thus use
incompatible hash codes.

When you issue a mixed-type comparison, one is bridged to the other.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-users mailing list