Skip to content

Add generic associatedtypes and generalized supertype constraints #13012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/GenericsManifesto.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,30 @@ var d1 = StringDictionary<Int>()
var d2: Dictionary<String, Int> = d1 // okay: d1 and d2 have the same type, Dictionary<String, Int>
```

### Generic associatedtypes

Associatedtypes could be allowed to carry generic parameters.

```Swift
protocol Wrapper {
associatedtype Wrapped<T>

static func wrap<T>(_ t: T) -> Wrapped<T>
}
```

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.

```Swift
enum OptionalWrapper {
typealias Wrapped<T> = Optional<T>

static func wrap<T>(_ t: T) -> Optional<T>
}
```

Note: generic associatedtypes address many use cases also addressed by higher-kinded types but with lower implementation complexity.

### Generic subscripts

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

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

### Generalized supertype constraints

Currently, supertype constraints may only be specified using a concrete class or protocol type. This prevents us from abstracting over the supertype.

```Swift
protocol P {
associatedtype Base
associatedtype Derived: Base
}
```

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.

Generalized supertype constraints would be accepted in all syntactic locations where generic constraints are accepted.

### Allowing subclasses to override requirements satisfied by defaults (*)

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:
Expand Down