[swift-users] Subtract a set of a subclass?

Howard Lovatt howard.lovatt at gmail.com
Wed Aug 31 21:55:34 CDT 2016

I am probably misunderstanding you, but it seems to work for me:

class A: Hashable, CustomStringConvertible {
    var hashValue: Int { return ObjectIdentifier(self).hashValue }
    var description: String { return "A@\(hashValue)" }
func ==(lhs: A, rhs: A) -> Bool {
    return lhs.hashValue == rhs.hashValue
class B: A {
    override var description: String { return "B@\(hashValue)" }

let ca = A() // Common A to be removed
let ra = A() // Retained A
let cb = B() // Common B to be removed
let rb = B() // Retained B
let aa: Set<A> = [ca, ra] // Have spelt out the type of aa, but compiler
will infer correctly
let ab: Set<A> = [ca, cb] // Have spelt out the type of ab, but compiler
will infer correctly
let a = aa.subtracting(ab)
a.contains(ca) // false
a.contains(ra) // true

let bb: Set<A> = [cb, rb] // Have to spell out the type of bb, compiler
will infer Set<B>
let b = bb.subtracting(ab)
b.contains(cb) // false
b.contains(rb) // true
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160901/53cb6f28/attachment.html>

More information about the swift-users mailing list