Swift, Foundation, Combine

70 views Asked by At

The Foundation framework in Swift imports Combine:

enter image description here

Why can't I use Combine when I only import Foundation?

By comparison, I can use the Foundation framework just by importing "UIKit" framework (which imports "Foundation" internally).

I'm trying to remove duplicated imports.

1

There are 1 answers

4
Alexander On

You don't usually want imports to work transitively like this.

Many libraries want to have a small public interface, while doing lots of stuff internally that they don't want others to worry about. It bloats your namespace, importing more things than you usually care for, which then slows IDE and compiler performance. (e.g. there will be more type available to look up).

When you do want it, there's the private @_exported attribute, which is what UIKit is using to re-export Foundation to its callers.

@_exported import ModuleName