[swift-users] Subtract a set of a subclass?
Zhao Xin
owenzx at gmail.com
Fri Sep 2 21:47:17 CDT 2016
You are correct. Whether override `hashValue` is basing on the
implementation of `==`. You can't do what you want. You should follow the
required rules. However, if you don't want to get the unexpected result,
you can always use a `convenience init(_ instance:Self)` in `extension` to
convert subclass to superclass as I did in previous replies.
Zhaoxin
On Sat, Sep 3, 2016 at 10:12 AM, Dmitri Gribenko <gribozavr at gmail.com>
wrote:
> On Sat, Sep 3, 2016 at 4:47 AM, Zhao Xin via swift-users
> <swift-users at swift.org> wrote:
> > Hi Jordan,
> >
> > Both you and I were wrong.
> >
> > My wrongness: Your so called principle should be applied here.
> >
> > Your wrongness: If you really want a different hash value, the parent
> > equality function has to be conservative and say that the different types
> > are different.
>
> That's one way you can satisfy the rules, but not the only one.
>
> Think about class clusters. NSString has many subclasses that store
> strings in different ways (for example, as Latin1 and UTF-16), but any
> subclass instance compares equal to any other subclass instance that
> carries the same character data, and also produces the same hash
> value.
>
> 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>*/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160903/d94c1c10/attachment.html>
More information about the swift-users
mailing list