- Published on
Simplified imports with @_exported attribute
- Authors
- Name
Swift, as a powerful language, possess many features that are not well known. One of them is the @_exported
attribute that allows you to import all the public declarations of a module into the current module.
When you are working with Swift, you need to import the modules that you are going to use. For example, if you want to use the Foundation
module, you need to import it:
import Foundation
This is a simple example, but when you are working with a big project, you need to import many modules, and this can be a little bit annoying.
The @_exported
attribute allows you to import all the public declarations of a module into the current module. This means that you can use the declarations of the module without importing it.
For example, if you want to use the Foundation
module, you can use the @_exported
attribute to import it:
@_exported import Foundation
This way, you can use the declarations of the Foundation
module without importing it within your current working module.
Another use case can be when you have a module that depends on another module. Instead of importing the dependent module in your module, you can use the @_exported
attribute to import it:
@_exported import ModuleC
import ModuleB
If you look for documentation about the @_exported
attribute, you will not find it. This is because it is an not widely documented feature of Swift. Either way you can find it inside swift repository under Underscored Attributes section, marked as Strongly discouraged
to use outside of the standard library.
The @_exported
attribute is a powerful feature of Swift that allows you to simplify the imports in your code. This can be very useful when you are working with a big project and you need to import many modules.
Be mindful when using it as it can lead to unexpected behavior being a non-stable attribute and it is prone to break in the future.