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/NNNN-non-escapable.md
+25-14Lines changed: 25 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -289,33 +289,44 @@ We expect to publish a sample implementation and proposal for that type very soo
289
289
290
290
#### Initializers and Lifetime Dependencies
291
291
292
-
All values come into existence within the body of some initializer and are returned to the caller of that initializer.
293
-
Since nonescapable types cannot be returned,
294
-
it follows that nonescapable types cannot have initializers without some additional language affordance.
295
-
296
-
A subsequent proposal will provide such an affordance.
297
-
This will allow values to be returned subject to the requirement that they not outlive some other specific value.
298
-
For example, a nonescapable iterator might be initialized so as to not outlive the container that created it:
292
+
Nonescapable function parameters may not outlive the function scope.
293
+
Consequently, nonescapable values can never be returned from a function.
294
+
Nonescapable values come into existence within the body of the initializer.
295
+
Naturally, the initializer must return its value, and this creates an exception to the rule.
296
+
Without further language support, implementing a nonescapable initializer requires an unsafe construct.
297
+
That unsafe handling is not covered by this proposal because we don't want to surface unnecessary unsafety in the language.
298
+
Instead, a subsequent proposal will support safe initialization by allowing the initializer to specify lifetime dependencies on its parameters.
299
+
The parameters to the initializer typically indicate a lifetime that the nonescapable value cannot outlive.
300
+
An initializer may, for example, create a nonescapable value that depends on a container variable that is bound to an object with its own lifetime:
299
301
```swift
300
302
structIterator: ~Escapable {
301
-
// ⚠️️ Returned Iterator will not be allowed to outlive `container`
302
-
// Details in a future proposal: This may involve
303
-
// additional syntax or default inference rules.
304
303
init(container: borrowing Container) { ... }
305
304
}
306
305
306
+
let container =...
307
+
let iterator =Iterator(container)
308
+
consume container
309
+
use(iterator) // 🛑 'iterator' outlives the source of its dependency
310
+
```
311
+
312
+
Lifetime dependencies will make this use of iterator a compile-time error.
313
+
Or, as part of implementing data type internals, a nonescapable initializer may depend on a variable that is bound to a value that is only valid within that variable's local scope.
314
+
Subsequent uses of the initialized nonescapable object are exactly as safe or unsafe as it would be to use the variable that the initializer depends at the same point:
315
+
316
+
```swift
307
317
let iterator: Iterator
308
318
do {
309
319
let container =Container(...)
310
320
let buffer = container.buffer
311
321
iterator =Iterator(buffer)
312
-
// `container` lifetime ends here
322
+
// `iterator` is safe as long as `buffer` is safe to use.
0 commit comments