Skip to content

Commit 2cff06d

Browse files
committed
Add generic associatedtypes and generalized supertype constraints
1 parent 28b471c commit 2cff06d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

docs/GenericsManifesto.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ var d1 = StringDictionary<Int>()
8888
var d2: Dictionary<String, Int> = d1 // okay: d1 and d2 have the same type, Dictionary<String, Int>
8989
```
9090

91+
### Generic associatedtypes
92+
93+
Associatedtypes could be allowed to carry generic parameters.
94+
95+
```Swift
96+
protocol Wrapper {
97+
associatedtype Wrapped<T>
98+
99+
static func wrap<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+
enum OptionalWrapper {
107+
typealias Wrapped<T> = Optional<T>
108+
109+
static func wrap<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+
91115
### Generic subscripts
92116

93117
*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.*
@@ -249,6 +273,21 @@ typealias AnyObject = protocol<class>
249273

250274
See the "Existentials" section, particularly "Generalized existentials", for more information.
251275

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+
protocol P {
282+
associatedtype Base
283+
associatedtype Derived: 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+
252291
### Allowing subclasses to override requirements satisfied by defaults (*)
253292

254293
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

Comments
 (0)