Struggling with Conformance by Proxy in Swift 6: Need Guidance

Here's the revised post for the forum, including the error message:


I read this topic and tried it in Xcode 16 beta 1.

Conformance by Proxy

It could be possible to use an intermediate type to help address static isolation differences. This can be particularly effective if the protocol requires inheritance by its conforming types.

class UIStyler {
}

protocol Styler: UIStyler {
    func applyStyle()
}

// actors cannot have class-based inheritance
actor WindowStyler: Styler {
}

Introducing a new type to conform indirectly can make this situation work. However, this solution will require some structural changes to WindowStyler that could spill out code that depends on it as well.

struct CustomWindowStyle: Styler {
    func applyStyle() {
    }
}

Here, a new type has been created that can satisfy the needed inheritance. Incorporating will be easiest if the conformance is only used internally by WindowStyler.

I faced the compile issue: Non-class type 'CustomWindowStyle' cannot conform to class protocol 'Styler'

I didn't fully understand this technique, so I'm not entirely sure how to use it. Perhaps this variant is what is intended:

class UIStyler {
}

protocol Styler {
    var uiStyler: UIStyler { get } // Protocol requirement
    func applyStyle()
}

struct CustomWindowStyle: Styler {
    var uiStyler: UIStyler = UIStyler() // Protocol conformance
    
    func applyStyle() {
        // Style implementation
    }
}

However, I am still not sure if this is the correct approach. Any guidance or suggestions would be greatly appreciated.


Yes, this text is quite the puzzler as it's invalid Swift... The guide is open-source and I've gone ahead a filed an issue:

3 Likes

Thank you, Xiaodi Wu, for filing the issue and bringing this to our attention. Your help is much appreciated.

Thanks so much for pointing this out. A fix has been proposed, but I'll include it here.

// class with necessary superclass
class CustomWindowStyle: UIStyler {
}

// now, the conformance is possible
extension CustomWindowStyle: Styler {
    func applyStyle() {
    }
}
1 Like