Generic constraint for "weakly referencable"

Continuing topic of Hashing Weak Variables.

I'm struggling to express a generic constraint that would cover class types and existential types for protocols with class constraint:

struct WeakRef<T: AnyObject> {
    weak var ref: T?
}

class MyClass {}
protocol P: AnyObject {}

weak var x1: MyClass? // OK
weak var x2: P? // OK
var wr1: WeakRef<MyClass> // OK
var wr2: WeakRef<P> // ERROR: 'WeakRef' requires that 'P' be a class type
Terms of Service

Privacy Policy

Cookie Policy