1010
1111## Introduction
1212
13- Today, it is not possible to declare an associated type that does not require its
14- _ type witnesses _ to be ` Copyable ` or ` Escapable ` . For example, consider the ` Element `
15- associated type of ` Queue ` below:
13+ An associated type defines a generic type in a protocol.
14+ You use them to help define the protocol's requirements.
15+ This Queue has two associated types, Element and Allocator:
1616``` swift
1717/// Queue has no reason to require Element to be Copyable.
1818protocol Queue <Element>: ~ Copyable {
@@ -22,20 +22,31 @@ protocol Queue<Element>: ~Copyable {
2222 init ()
2323 init (alloc : Allocator)
2424
25- mutating func push (_ : Self .Element )
26- mutating func pop () -> Self .Element
25+ mutating func push (_ : Element )
26+ mutating func pop () -> Element
27+ // ...
2728}
2829```
30+ The first associated type Element represents the type of value that by which
31+ ` push ` and ` pop ` must be defined.
32+
33+ Any type conforming to Queue must define a nested type Element that satisfies
34+ the protocol's requirements, of which there are no _ explicit_ requirements.
2935While the conforming type is itself permitted to be noncopyable, its ` Element `
30- type witness has to be ` Copyable ` :
36+ type has to be ` Copyable ` :
3137``` swift
3238/// error: LinkedList does not conform to Queue
39+ /// note: Element is required to be Copyable
3340struct LinkedList <Element : ~ Copyable >: ~ Copyable , Queue {
3441 ...
3542}
3643```
37- This is an expressivity limitation in practice, and there is no workaround
38- possible today.
44+ This is because in [ SE-427: Noncopyable Generics] ( 0427-noncopyable-generics.md ) ,
45+ an implicit requirement that ` Queue.Element: Copyable & Escapable ` is inferred,
46+ with no way to suppress it in that protocol.
47+ This is expressivity limitation in practice, as it prevents Swift programmers
48+ from defining protocols that work with noncopyable or nonescapable associated
49+ types.
3950
4051## Proposed Solution
4152
@@ -56,8 +67,8 @@ protocol Queue<Element>: ~Copyable {
5667}
5768```
5869
59- Now, ` LinkedList ` can conform to ` Queue ` , as its witness for
60- ` Queue.Element ` is not required to be ` Copyable ` .
70+ Now, ` LinkedList ` can conform to ` Queue ` , as its ` Element ` is not required to be
71+ ` Copyable ` .
6172Similarly, stating ` ~Escapable ` is allowed, to suppress the default
6273conformance requirement for ` Escapable ` .
6374Unless otherwise noted, any discussion of ` ~Copyable ` types applies equivalently
0 commit comments