Redundent extension for protocol conformance in Swift

  Kiến thức lập trình
protocol EmptyInitializable {
    init()
}

@propertyWrapper
struct PropertyWrapper: EmptyInitializable {
    let wrappedValue: Int
    
    init(_ wrappedValue: Int = 0) {
        self.wrappedValue = wrappedValue
    }
}

// This one lools redundent
extension PropertyWrapper {
    public init() {
        wrappedValue = 0
    }
}

Why do we need that extansion at the bottom? Why can’t compiler just figure out, that struct PropertyWrapper already complies with the requirements for protocol EmptyInitializable? It looks so ugly…

P.S. I beleive that it actually used to work in the similar situations before.

LEAVE A COMMENT