Skip to content

Commit 949f98b

Browse files
committed
[SE-0478] Add an alternative for using defaultIsolation together
with `using` instead of plain attributes and modifiers.
1 parent 864501f commit 949f98b

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

proposals/0478-default-isolation-typealias.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,59 @@ This proposal does not change the adoption implications of adding `@MainActor` t
128128

129129
## Alternatives considered
130130

131+
### Using Swift package manifest-style APIs for specifying default attributes
132+
133+
Instead of supporting attributes and modifiers directly, we could instead use Swift package manifest-style APIs for specifying default attributes. For example:
134+
135+
```swift
136+
using defaultIsolation(MainActor.self)
137+
```
138+
139+
We did not choose this direction for two reasons:
140+
1. The attribute or modifier written after `using` makes it immediately clear what kind of default we're specifying. `@MainActor` and `nonisolated` are understood to be kinds of isolation, so having to write "isolation" in the syntax is not clarifying.
141+
2. Having to write "default" in the syntax is equally not clarifying, and it will be repetitive if `using` is extended ot other attributes.
142+
143+
To elaborate on these points, consider the future direction to extend `using` to `@available` attributes:
144+
145+
```swift
146+
using @available(SwiftStdlib 5.1, *)
147+
148+
// Has default Swift 5.1 availability
149+
public protocol Actor { ... }
150+
151+
// All concurrency-related APIs
152+
```
153+
154+
It is immediately clear that what's being specified is availability, so having to include an additional "availability" in the syntax is repetitive:
155+
156+
```swift
157+
using defaultAvailability("@available(SwiftStdlib 5.1, *)")
158+
```
159+
160+
Not all attributes have a value representation in Swift code, so we'd likely end up having to write attributes and modifiers in string literals, which is not as nice as writing a plain attribute or modifier.
161+
162+
How the attribute or modifier is applied as a default depends on how inference for that attribute or modifier works throughout the language. For availability, if you write an extension of a less-available type in the same file, the extension will still have the more constrained availability:
163+
164+
```swift
165+
using @available(SwiftStdlib 5.1, *)
166+
167+
// Has default Swift 5.1 availability
168+
public protocol Actor { ... }
169+
170+
// Has default Swift 5.1 availability
171+
@globalActor
172+
public actor MainActor { ... }
173+
174+
// Has explicit Swift 6.0 availability
175+
@available(SwiftStdlib 6.0, *)
176+
public protocol TaskExecutor { ... }
177+
178+
// Has implicit Swift 6.0 availability
179+
extension TaskExecutor { ... }
180+
```
181+
182+
The fact that attributes and modifiers are only used as defaults will apply to every attribute or modifier that `using` is extended to. Programmers will learn this once when encountering `using` for the first time, and having to repeat the word `default` in the syntax with every use will not help reinforce how the default is applied.
183+
131184
### A typealias to specify default isolation per file
132185

133186
A previous iteration of this proposal used a typealias to specify default actor isolation instead of a new syntax:
@@ -178,7 +231,9 @@ However, default actor isolation has a significant difference from the other com
178231

179232
## Future directions
180233

181-
While there is no immediate future use case, `using` declarations in this proposal are designed so that they can be extended later if we need to. `using` is a general name that can work with any attribute or modifier. It can also be extended to work with SwiftPM manifest-style APIs, e.g.
234+
`using` declarations in this proposal are designed so that they can be extended later if we need to. `using` is a general name that can work with any attribute or modifier. One compelling use case is to support `@available` attributes, which already effectively have a module-wide default based on deployment target.
235+
236+
`using` can also be extended to work with other SwiftPM manifest-style APIs, e.g.
182237

183238
```swift
184239
using strictMemorySafety()

0 commit comments

Comments
 (0)