Importing extensions

I've got a possibly dumb (or possibly overly vague) question about how the Swift compiler works. Especially when it comes to using a library with extensions to other libraries.

I use ReactiveSwift in a lot of projects. And it comes with extensions to Foundation types. For example I have some code to observe a notification type from Notification Center.

 NotificationCenter.default.reactive.notifications(forName: UIApplication.willTerminateNotification)
    .take(duringLifetimeOf: self)
    .observeValues { [weak self] _ in 
        // Do some stuff
    }

This extension is defined in the ReactiveSwift library, but I don't have to import ReactiveSwift use it. If I were to try to instantiate a ReactiveSwift defined type in that same file, I would get a "Cannot find SignalProducer in scope" error. Which would easily be resolved by importing ReactiveSwift

I guess my question is, what enables that behavior? Should I be importing ReactiveSwift anyways or am I fine to rely on this behavior.

1 Like