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

Karl Wagner razielim at gmail.com
Mon Feb 6 23:12:02 CST 2017


> On 7 Feb 2017, at 06:05, Slava Pestov <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
        func item(at: Int) -> MediaItem
        var numberOfItems: Int { get }
    }
    var source: Any<MyCollectionView.Source where .MediaItem == MediaItem> // Not possible today.
}

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/20170207/f3929bd2/attachment.html>


More information about the swift-evolution mailing list