Subtract a set of a subclass?


(Howard Lovatt) #1

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