Jayden Irwin
Jayden Irwin

Reputation: 977

Module 'Swift' has no member named 'Task'

I have created an enum named Task in my project. Now I want to use the Swift Task (https://developer.apple.com/documentation/swift/task).

When I want to use Swift Task I wrote: Swift.Task { ... } but I get the error: Module 'Swift' has no member named 'Task'

Upvotes: 7

Views: 1826

Answers (1)

Itai Ferber
Itai Ferber

Reputation: 29918

In order to support backdeployment of Swift concurrency features to older operating systems, Swift builds the concurrency APIs into a separate library which can be shipped separately from the Swift standard library. This swift_Concurrency library is exposed as an implicit module called _Concurrency, which is re-exported from the Swift standard library.

However, the Swift concurrency classes and types still belong to this implementation-only library, so in order to disambiguate, you'll need to write _Concurrency.Task instead of Swift.Task.

You can also see this in Xcode: if you refer to Task in a new project, and ^-⌘-click on the symbol to jump to its definition, you'll find it in the _Concurrency library interface:

Task interface definition in Xcode


Instead of explicitly writing out _Concurrency.Task in order to disambiguate everywhere, consider the following:

  1. You can create a typealias for the type (e.g. typealias SwiftTask = _Concurrency.Task) if you prefer your own Task type taking precedence, and don't want to rename it
  2. Alternatively, depending on whether it makes sense for your project (and is reasonable in context): this might be an opportunity to consider a more specific name for your type to possibly avoid both nominal and logical conflicts. async/await and Task are major new language/stdlib features that are going to be influential for a long time, so it may make sense to pick a less general word if the stdlib is going to be reserving that one going forward

Upvotes: 14

Related Questions