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.