Underscore attributes: @_exported


Greetings, traveler!

Modular architectures are becoming increasingly popular. Many developer teams are creating their apps based on different modules. While this approach helps to gain code independence and reuse modules in other projects, it can lead to some challenges.

Sometimes, we need to make a lot of imports at the beginning of the file, even though some imported modules are bound with others. But there is a way to reduce the count of imports with the @_exported attribute.

Note

There is a warning in Swift documentation about underscore attributes:

Usage of these attributes outside of the Swift monorepo is STRONGLY DISCOURAGED.

The semantics of these attributes are subject to change and most likely need to go through the Swift evolution process before being stabilized.

All the stable attributes are described here.

Example

In Swift, @_exported can help us make all the interfaces of one module available while importing another. So, instead of writing this:

import ModuleA
import ModuleB

You can write this code in the ModuleA:

@_exported import ModuleB

After that, you can use ModuleB with ModuleA without explicitly importing it.

import ModuleA

Conclusion

While this approach can seem like a convenient tool, we should remember that it can be unstable. That’s why this attribute is marked as underscored.