Import detection for compatibility purposes

I have been considering if #if imported() would be a possible and reasonable addition to Swift. The syntax w

struct RandomType { … }
#if imported(SomeProtocolPackage)
extension RandomType: SomeProtocol {}
#endif

As the current method (as far as I know) is:

#if canImport(SomeProtocolPackage)
import SomeProtocolPackage
extension RandomType: SomeProtocol {}
#endif

I am putting forward that when the imported package/module is accessible this code would function and be compiled and implemented into the code

Note:
A syntax error is only raised if the Package(potentially via .modulemap) doesn’t expose this protocol
The code will be parsed as though the module is imported to validate that the code could possibly be compiled.
Package specific Macros may require you to fully test the code with the full module as macros are not easily reproducible from interface representations.

I do however see a potential issue which is that the compiler would need to package discardable code for a future compilation to resolve and this would need to be present recursively though dependencies in the event that someone wants to import the related package. This could be solved by introducing something like -drop-unused-compat as a compiler option that makes the resulting code ignore any discardable compatibility code. After -drop-unused-compat if the condition would evaluate true that code will not be implemented into the source tree as it is no longer passed post flagged compilation. There are also security concerns that discardable code could be used to bypass or otherwise gain control of sensitive code execution environments.

This change may result in build system changes for SwiftPM and XCode’s build system to add support for compatibilities.

Do you know about SwiftPM Package Traits? If so, please elaborate on how the two differ.

So I could do something like this with the following in Package.swift

dependencies: [
    .package(
        url: "https://github.com/Org/SomePackage.git",
        from: "1.0.0",
        traits:[
            .trait("SomePackIntegratePackage", condition: .when(traits: ["IntegrateIndicator"]) )
        ]
    ),
    .package(
        url: "https://github.com/Org/IntegratablePackage.git",
        from: "1.0.0",
        traits:[
            .trait("IntegrateIndicator")
        ]
    )
]

And then use it like so?

#if SomePackIntegratePackage
    import IntegratePackage
    IntegratePackage.desiredCode()
#else
    print("Failed to detect IntegratePackage")
#endif

I don't have personal experience using package traits yet but I would assume yes. I would personally move the second dependency above the other one just to make it more understandable.