[swift-evolution] [Idea] Generic associated types

Matthew Johnson matthew at anandabits.com
Sun Mar 12 08:32:02 CDT 2017


This is a really important feature IMO, but as others have pointed out it basically amounts to higher-kinded types.  I would love to be wrong about this but I am reasonably sure this is out of scope for Swift 4 (otherwise I would be working on a proposal already).

Sent from my iPad

> On Mar 11, 2017, at 11:49 PM, Karl Wagner via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170312/c3345441/attachment.html>


More information about the swift-evolution mailing list