[swift-evolution] [Idea] Generic associated types

Karl Wagner razielim at gmail.com
Sat Mar 11 23:49:29 CST 2017


I have a model like this:

protocol Promise {
    associatedtype Result
}

protocol Scanner {
    associatedtype ScanPromise: Promise

    func promiseScan<T>(from: Offset, until: (Offset, Item) -> T?) -> ScanPromise // where Result == T?
}

The thing that I’m trying to express is: whichever type implements the associated type ‘ScanPromise’ must be generic, and that parameter must be its result (i.e. something it got as a result of calling the “until” closure).

Even with SE-0142, this kind of constraint would not be possible. What I would like to write is something like this:

protocol Promise {
    associatedtype Result
}

protocol Scanner {
    associatedtype ScanPromise<T>: Promise // now generic. [SE-0142]: where Result == T

    func promiseScan<T>(from: Offset, until: (Offset, Item) -> T?) -> ScanPromise<T>
}

Thoughts?

- Karl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170312/a61a2416/attachment.html>


More information about the swift-evolution mailing list