# Conditional Imports

**URL:** <https://forums.swift.org/t/conditional-imports/66934>\
**Category:** Using Swift\
**Created:** [August 24, 2023, 11:04pm UTC](https://forums.swift.org/t/conditional-imports/66934 "2023-08-24T23:04:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdotclare](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tdotclare/32/11455_2.png) [@tdotclare](https://forums.swift.org/u/tdotclare)\
**Post date:** [August 24, 2023, 11:04pm UTC](https://forums.swift.org/t/conditional-imports/66934/1 "2023-08-24T23:04:54Z")

</div>

Hi - have been out of the game for a while, and am having trouble figuring out if this is feasible now -

Can a package _conditionally_ import dependencies _only_ if a user of the package has already imported that dependency?

EG; let's say I have a package that by itself does not rely on Foundation, so I don't want to import it. However, I'd like to include additional functions if Foundation _is_ already imported elsewhere, without requiring a build of the package to automatically link to Foundation.

Is the only alternative for something like this along the lines of SwiftNIO having a "NIOFoundationCompat" target?

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [August 24, 2023, 11:37pm UTC](https://forums.swift.org/t/conditional-imports/66934/2 "2023-08-24T23:37:55Z")

</div>

The feature you're looking for is [cross-import overlays](https://forums.swift.org/t/cross-import-overlays/36710), but it's only been pitched and not officially supported\* yet.

\* The compiler supports it for system frameworks, but since it hasn't gone through Swift Evolution yet, its implementation is subject to change and it doesn't work with Swift Package Manager.

---

<div class="post-metadata">

**Author:** ![tdotclare](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tdotclare/32/11455_2.png) [@tdotclare](https://forums.swift.org/u/tdotclare)\
**Post date:** [August 25, 2023, 12:53am UTC](https://forums.swift.org/t/conditional-imports/66934/3 "2023-08-25T00:53:13Z")

</div>

Interesting - that might be an answer, though from a brief glance it seems like it's solving a more complicated set of conditions?

Just wish there was a more straightforward method - eg something like this, particularly for system libraries so you could explicitly prevent them from being linked automatically. Wouldn't actually be source-breaking as far as I can tell and would allow more granular control over how packages link against system libraries in addition to external packages, and would probably make `#if canImport` behave more like (I suspect) most people expect it to when used.

```swift
/// in Package.swift
...
dependencies: [.optionalPackage(name: "Foundation")],
...
targets: [.target(name: "MyLib", optionalDependencies: ["Foundation"])
...

/// MyLib.swift
#if canImport("Foundation") 
import Foundation
#endif

```
