# Implicitly imported types & namespace priority

**URL:** <https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928>\
**Category:** Using Swift\
**Tags:** concurrency, swiftui, type-inference, namespaces\
**Created:** [June 8, 2022, 11:42pm UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928 "2022-06-08T23:42:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lhunath](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lhunath/32/18794_2.png) [@lhunath](https://forums.swift.org/u/lhunath)\
**Post date:** [June 8, 2022, 11:42pm UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/1 "2022-06-08T23:42:06Z")

</div>

The `SwiftUI` module implicitly includes the private `_Concurrency` module, such that the full Swift Concurrency API is available to us from SwiftUI views.

```swift
import SwiftUI

Task {
    await withCheckedContinuation { $0.resume() }
}

```

However, when we encounter an environment where a competing type name exists either in the module or even in a separate module that is also imported, the competing type always appears to take precedence:

```swift
// MyModule
public protocol Task {}

```

```swift
import SwiftUI
import MyModule

Task { // 'Task' cannot be constructed because it has no accessible initializers
    await withCheckedContinuation { $0.resume() }
}

```

We can force Swift to find the correct `Task` by prefixing using the private namespace `_Concurrency`:

```swift
import SwiftUI
import MyModule

_Concurrency.Task {
    await withCheckedContinuation { $0.resume() }
}

```

However, this does not look kosher and we are now using underscored symbols in our code.

What would be better is if we could allow "language types" to take precedence over our own types. I had half-expected to be able to do this by sorting the imports:

```swift
import MyModule
import SwiftUI

Task { // 'Task' cannot be constructed because it has no accessible initializers
    await withCheckedContinuation { $0.resume() }
}

```

Alas, no such luck. Even explicitly bringing in `import _Concurrency` here does not appear to help.

Can anyone recommend a solution that avoids the need for underscored symbols and doesn't force me to rename my own API simply because Swift decided to rename `async` to `Task`?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 9, 2022, 2:22am UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/2 "2022-06-09T02:22:24Z")

</div>

> [@lhunath](#):
>
> The `SwiftUI` module implicitly includes the private `_Concurrency` module, such that the full Swift Concurrency API is available to us from SwiftUI views.

Actually, the Swift standard library is what re-exports the `_Concurrency` module [edit: actually, I guess it isn't really a re-export, as you can't refer to `Task` as `Swift.Task`]; you don't need to write any explicit import statement to use the `Task` API.

> [@lhunath](#):
>
> What would be better is if we could allow "language types" to take precedence over our own types.

There is a special case in the compiler specifically to do the opposite: that is, it's deliberate that user types are always favored. Otherwise, every addition to the Swift standard library would be a possibly source-breaking change.

> [@lhunath](#):
>
> Can anyone recommend a solution that avoids the need for underscored symbols and doesn't force me to rename my own API simply because Swift decided to rename `async` to `Task` ?

In a separate file within your project, write (without including anything else in the file):

```swift
typealias ConcurrencyTask /* or whatever you want to call it */ = Task

```

You can then use that alias everywhere in your project.

---

<div class="post-metadata">

**Author:** ![lhunath](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lhunath/32/18794_2.png) [@lhunath](https://forums.swift.org/u/lhunath)\
**Post date:** [June 9, 2022, 3:06am UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/3 "2022-06-09T03:06:07Z")

</div>

> [@xwu](#):
>
> In a separate file within your project, write (without including anything else in the file):
> 
> ```swift
> typealias ConcurrencyTask /* or whatever you want to call it */ = Task
> 
> ```
> 
> You can then use that alias everywhere in your project.

Thanks; I had considered a typealias as well, though I wasn't sure how I felt about redefining standard types into non-standard names. It seems like this would lead to inevitable confusion for other or new developers on the team and antithetical to having a "standard library" of universally recognized language features.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 9, 2022, 3:44am UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/4 "2022-06-09T03:44:10Z")

</div>

> [@lhunath](#):
>
> Thanks; I had considered a typealias as well, though I wasn't sure how I felt about redefining standard types into non-standard names. It seems like this would lead to inevitable confusion for other or new developers on the team and antithetical to having a "standard library" of universally recognized language features.

Your three and only three options here are: rename your own `Task` type, rename Swift's `Task` type, or fully qualify the latter by writing `_Concurrency.Task`.

---

<div class="post-metadata">

**Author:** ![lhunath](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lhunath/32/18794_2.png) [@lhunath](https://forums.swift.org/u/lhunath)\
**Post date:** [June 11, 2022, 12:13am UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/5 "2022-06-11T00:13:30Z")

</div>

Is `_Concurrency` a safe symbol to reference? (ie. is its name "public" & stable)

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [June 11, 2022, 11:39am UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/6 "2022-06-11T11:39:01Z")

</div>

Unless you need that other `Task`, you can also restrain your imports by `import struct MyModule.MyStruct`, thereby avoiding an implicit import of `MyModule.Task`.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [June 11, 2022, 6:58pm UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/7 "2022-06-11T18:58:43Z")

</div>

> [@xwu](#):
>
> Your three and only three options here are: rename your own `Task` type, rename Swift's `Task` type, or fully qualify the latter by writing `_Concurrency.Task` .

> [@lhunath](#):
>
> Is `_Concurrency` a safe symbol to reference? (ie. is its name "public" & stable)

`_Concurrency.Task` is the correct invocation. the underscore is [part of the module name](https://swiftinit.org/reference/concurrency), do not fear the underscore just because it’s there.

however, you may eventually experience some source breakage once the core team decides to drop the underscore, similar to `_Distributed` `->` [`Distributed`](https://swiftinit.org/reference/distributed). but this would also happen if you used the typealias.

---

<div class="post-metadata">

**Author:** ![yo1995](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/yo1995/32/16740_2.png) [@yo1995](https://forums.swift.org/u/yo1995)\
**Post date:** [October 20, 2022, 8:42pm UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/8 "2022-10-20T20:42:04Z")

</div>

> There is a special case in the compiler specifically to do the opposite: that is, it's deliberate that user types are always favored.

Hi Xiaodi, can you point me to the src of this behavior?

My understanding to what you said is that, for example, instead of specifying both `SwiftUI.Scene` and `MyModule.Scene`, the expected behavior is to only specify `SwiftUI.Scene` and our type is implicitly favored as `Scene`.

Is that correct? Because Xcode is showing the internal error - bug report banner and the "Command CompileSwift failed with a nonzero exit code" error when our type name collides with a Swift's type name, if we don't explicitly put it as `MyModule.Scene`.

Thanks.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [October 20, 2022, 9:46pm UTC](https://forums.swift.org/t/implicitly-imported-types-namespace-priority/57928/9 "2022-10-20T21:46:28Z")

</div>

> [@yo1995](#):
>
> My understanding to what you said is that, for example, instead of specifying both `SwiftUI.Scene` and `MyModule.Scene`, the expected behavior is to only specify `SwiftUI.Scene` and our type is implicitly favored as `Scene`.
> 
> Is that correct?

No, there is special treatment for Swift standard library types only. SwiftUI is just a module like any other.
