[swift-evolution] [RFC][Proposal] Ease restrictions on protocol nesting

Slava Pestov spestov at apple.com
Mon Feb 6 23:18:45 CST 2017


> On Feb 6, 2017, at 9:12 PM, Karl Wagner <razielim at gmail.com> wrote:
> 
> 
>> On 7 Feb 2017, at 06:05, Slava Pestov <spestov at apple.com <mailto:spestov at apple.com>> wrote:
>> 
>>> 
>>> On Feb 6, 2017, at 9:00 PM, Karl Wagner via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>> - Nested protocols in generic types are not parameterised by the parent's generic parameters.
>> So if I write GenericType<Int>.SomeProto and GenericType<String>.SomeProto, is it the same protocol? What about GenericType.SomeProto, is that allowed?
>> 
>> Slava
> 
> GenericType.SomeProto (without parameters) is the only spelling that is allowed. There is no GenericType<Int>.SomeProto.
> 
> That way we avoid every bound-generic type creating a new protocol. 
> I think it works really nicely when you consider what it would like like with existential-based capturing. Notice that there is only one ‘MyCollectionView.Source’, and compatibility is determined based on existential constraints.
> 
> - Karl
> 
> 
> class MyCollectionView<MediaItem> : UICollectionView {
> 
>     protocol Source {
>         // [implicit] associatedtype MediaItem

I’m worried this is going to be tricky to implement; we definitely won’t get this with the initial implementation of nested protocol types.


>         func item(at: Int) -> MediaItem
>         var numberOfItems: Int { get }
>     }
>     var source: Any<MyCollectionView.Source where .MediaItem == MediaItem> // Not possible today.

I think for this use-case, it makes sense to allow the protocol itself to be parametrized.

Slava

> }
> 
> class BookSource: MyCollectionView.Source {
>     typealias MediaItem = Book
> 
>     func item(at: Int) -> Book { /* ... */ }
>     var numberOfItems: Int     { /* ... */ }
> }
> 
> class DummySource<MediaItem>: MyCollectionView.Source where MediaItem: DummyConstructable {
>     // associatedtype 'MediaItem' bound to generic parameter.
> 
>     func item(at: Int) -> MediaItem { /* ... */ }
>     var numberOfItems: Int          { /* ... */ } 
> }
> 
> MyCollectionView<Book>().source = BookSource()
> MyCollectionView<Book>().source = DummySource<Book>()
> MyCollectionView<Song>().source  = DummySource() // type is: DummySource<Song>
> MyCollectionView<Movie>().source = DummySource() // type is: DummySource<Movie>
> 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170206/1a663526/attachment.html>


More information about the swift-evolution mailing list