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
- Use `taking` and `borrowing func` modifiers to control `self`'s convention
- Add "alternative considered" for whether `take`n parameters should be bound
as mutable in the callee
@@ -237,27 +305,6 @@ implicitly copyable values), and that explicit operators on the call site
237
305
can be used in the important, but relatively rare, cases where the default
238
306
optimizer behavior is insufficient to get optimal code.
239
307
240
-
### Applying `borrow` and `take` modifiers to the `self` parameter of methods
241
-
242
-
This proposal does not yet specify how to control the calling convention of
243
-
the `self` parameter for methods. As currently implemented, the `__consuming`
244
-
modifier can be applied to the method declaration to make `self` be taken,
245
-
similar to how the `mutating` method modifier makes `self` be `inout`. We could
246
-
continue that scheme with new function-level modifiers:
247
-
248
-
```
249
-
struct Foo {
250
-
mutating func mutate() // self is inout
251
-
taking func take() // self is take
252
-
borrowing func borrow() // self is borrow
253
-
}
254
-
```
255
-
256
-
Alternatively, we could explore schemes to [allow the `self` parameter to be
257
-
declared explicitly](https://forums.swift.org/t/optional-explicit-self-parameter-declaration-in-methods/59522), which would allow for the `take` and `borrow`
258
-
modifiers as proposed for other parameters to also be applied to an explicit
259
-
`self` parameter declaration.
260
-
261
308
## Related directions
262
309
263
310
### `take` operator
@@ -271,7 +318,7 @@ last use, without depending on optimization and vague ARC optimizer rules.
271
318
When the lifetime of the variable ends in an argument to a `take` parameter,
272
319
then we can transfer ownership to the callee without any copies:
273
320
274
-
```
321
+
```swift
275
322
funcconsume(x: take Foo)
276
323
277
324
funcproduce() {
@@ -293,7 +340,7 @@ instead of passing a copy, then the mutation of `global` inside of `useFoo`
293
340
would trigger a dynamic exclusivity failure (or UB if exclusivity checks
294
341
are disabled):
295
342
296
-
```
343
+
```swift
297
344
var global =Foo()
298
345
299
346
funcuseFoo(x: borrow Foo) {
@@ -325,7 +372,7 @@ not attempt to modify the same object or global variable during a call,
325
372
and want to suppress this copy. An explicit `borrow` operator could allow for
326
373
this:
327
374
328
-
```
375
+
```swift
329
376
var global =Foo()
330
377
331
378
funcuseFooWithoutTouchingGlobal(x: borrow Foo) {
@@ -354,7 +401,7 @@ take a value destroy it and prevent its continued use. This makes the
354
401
convention used for move-only parameters a much more important part of their
355
402
API contract:
356
403
357
-
```
404
+
```swift
358
405
moveonly structFileHandle { ... }
359
406
360
407
// Operations that open a file handle return new FileHandle values
0 commit comments