Why can't we call superclass convenience initializer from subclass?

Oh I see. With automatic initializer inheritance, a convenience initializer may call designated initializers from subclass:

class A {
    
    init(x: Int) {
        print("A.init(x:)")
    }
    
    convenience init(y: Int) {
        print("A.init(y:)")
        self.init(x: y)
    }
}

class B: A {
    
    override init(x: Int) {
        print("B.init(x:)")
        super.init(x: x)
    }
}

B(y: 0) // A.init(y:) -> B.init(x:) -> A.init(x:)
// Allowing `B.init(x:)` to call `A.init(y:)` will cause an infinite loop.

(That's quite complicated...I found the Initialization section is the longest section in Swift Language Guide, about 1.5 times longer than Generics section)

3 Likes