You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0478-default-isolation-typealias.md
+56-1Lines changed: 56 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -128,6 +128,59 @@ This proposal does not change the adoption implications of adding `@MainActor` t
128
128
129
129
## Alternatives considered
130
130
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
+
publicprotocolActor { ... }
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
+
publicprotocolActor { ... }
169
+
170
+
// Has default Swift 5.1 availability
171
+
@globalActor
172
+
publicactorMainActor { ... }
173
+
174
+
// Has explicit Swift 6.0 availability
175
+
@available(SwiftStdlib 6.0, *)
176
+
publicprotocolTaskExecutor { ... }
177
+
178
+
// Has implicit Swift 6.0 availability
179
+
extensionTaskExecutor { ... }
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
+
131
184
### A typealias to specify default isolation per file
132
185
133
186
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
178
231
179
232
## Future directions
180
233
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.
0 commit comments