[swift-evolution] [Idea] Generic associated types

Xiaodi Wu xiaodi.wu at gmail.com
Sun Mar 12 01:21:49 CST 2017


Sorry, I'm confused. The following works:

```

protocol Promise {

  associatedtype Result

}


protocol Scanner {

  associatedtype ScannerPromise : Promise

  func frobnicate<T>(_: T) -> ScannerPromise

    where ScannerPromise.Result == T

}
```

Why does it matter if `ScannerPromise` is generic or not?


On Sat, Mar 11, 2017 at 11:55 PM, Karl Wagner via swift-evolution <
swift-evolution at swift.org> wrote:

>
> On 12 Mar 2017, at 06:51, Austin Zheng <austinzheng at gmail.com> wrote:
>
> I think you want higher-kinded types. https://github.com/
> apple/swift/blob/master/docs/GenericsManifesto.md#higher-kinded-types
>
> Best,
> Austin
>
> On Mar 11, 2017, at 9: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
>
>
>
>
> Not necessarily. Higher-kinded types (IIUC) are per-instance - e.g. every
> instance of a Collection could have a unique type of Index.
>
> I want to constrain a return value, whose type is an associated type,
> based on a generic parameter to the function. SE-0142 only allows
> constraining entire associated types, and allows no interaction with
> per-function generic parameters.
>
> - 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/3a99bcd0/attachment.html>


More information about the swift-evolution mailing list