hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.93k stars 116 forks source link

Swift 6: Warning for Retroactive Conformances of External Types #217

Closed jaseilers closed 3 months ago

jaseilers commented 3 months ago

Declaring the conformance AutoRegistering to Container triggers the accepted proposal retroactive conformances warning in Swift 6:

Extension declares a conformance of imported type 'Container' to imported protocol 'AutoRegistering'; this will not behave correctly if the owners of 'Factory' introduce this conformance in the future

I'm aware that retroactive conformance is an API design choice in Factory and we won't run into the pitfall of multiple conformances since it wouldn't make sense for the library to add conformance here. But since it's discouraged by the Swift evolution: Do you intend to change features like AutoRegistering to fix it or will @retroactive be the recommended solution?

hmlongco commented 3 months ago

Played around with a few other possible solutions, but still feel AutoRegistering is best. Added the following to the Container documentation.

AutoRegister and the Default Container

One can add auto registration capabilities to Factory's default container as well, but as of Swift 6 you'll need to add the @retroactive attribute to the AutoRegistering protocol definition.

extension Container: @retroactive AutoRegistering {
    func autoRegister() {
        someService.register { ModuleB.SomeService() }
    }
}

This new attribute silences the warning, "Extension declares a conformance of imported type 'Container' to imported protocol 'AutoRegistering'; this will not behave correctly if the owners of 'Factory' introduce this conformance in the future".

Yeah. Me too. I know the developers of Swift mean well, but sometimes...