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

Hello. I'm still confusing about what this topic(Conformance by Proxy) want to say, and what 'static isolation' means.
They want to say the actor can't inherit the class? or what?
Could anyone explain about this?

Static isolation refers to isolation applied using the type system. An example is a @MainActor annotation, or an actor type.

If you want to conform to a protocol that requires a base type, you cannot use an actor to do it. Actors cannot inherit from a Swift class.

This isn't a very common situation, but it happens. And when it does, this can be a good option to keep in mind.

1 Like