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: docs/GenericsManifesto.md
+39Lines changed: 39 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -88,6 +88,30 @@ var d1 = StringDictionary<Int>()
88
88
var d2: Dictionary<String, Int> = d1 // okay: d1 and d2 have the same type, Dictionary<String, Int>
89
89
```
90
90
91
+
### Generic associatedtypes
92
+
93
+
Associatedtypes could be allowed to carry generic parameters.
94
+
95
+
```Swift
96
+
protocolWrapper {
97
+
associatedtypeWrapped<T>
98
+
99
+
staticfuncwrap<T>(_t: T) -> Wrapped<T>
100
+
}
101
+
```
102
+
103
+
Generic associatedtypes would support all constraints supported by the language including where clauses. As with non-generic associatedtypes conforming types would be required to provide a nested type or typealias matching the name of the associatedtype. However, in this case the nested type or typealias would be generic.
104
+
105
+
```Swift
106
+
enumOptionalWrapper {
107
+
typealiasWrapped<T> =Optional<T>
108
+
109
+
staticfuncwrap<T>(_t: T) ->Optional<T>
110
+
}
111
+
```
112
+
113
+
Note: generic associatedtypes address many use cases also addressed by higher-kinded types but with lower implementation complexity.
114
+
91
115
### Generic subscripts
92
116
93
117
*This feature has been accepted in [SE-0148](https://github.com/apple/swift-evolution/blob/master/proposals/0148-generic-subscripts.md), was tracked by [SR-115](https://bugs.swift.org/browse/SR-115) and was released with Swift 4.*
See the "Existentials" section, particularly "Generalized existentials", for more information.
251
275
276
+
### Generalized supertype constraints
277
+
278
+
Currently, supertype constraints may only be specified using a concrete class or protocol type. This prevents us from abstracting over the supertype.
279
+
280
+
```Swift
281
+
protocolP {
282
+
associatedtypeBase
283
+
associatedtypeDerived: Base
284
+
}
285
+
```
286
+
287
+
In the above example `Base` may be any type. `Derived` may be the same as `Base` or may be _any_ subtype of `Base`. All subtype relationships supported by Swift should be supported in this context including (but not limited to) classes and subclasses, existentials and conforming concrete types or refining existentials, `T?` and `T`, `((Base) -> Void)` and `((Derived) -> Void)`, etc.
288
+
289
+
Generalized supertype constraints would be accepted in all syntactic locations where generic constraints are accepted.
290
+
252
291
### Allowing subclasses to override requirements satisfied by defaults (*)
253
292
254
293
When a superclass conforms to a protocol and has one of the protocol's requirements satisfied by a member of a protocol extension, that member currently cannot be overridden by a subclass. For example:
0 commit comments