Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-11021] Not getting the correct type from a protocol in generic Collection Element type #53411

Open
swift-ci opened this issue Jun 26, 2019 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11021
Radar None
Original Reporter marvinz (JIRA User)
Type Bug
Environment

Xcode Version 11.0 beta 2 (11M337n)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, DiagnosticsQoI
Assignee @xedin
Priority Medium

md5: 670c5b3d42916977516fac69de210c34

Issue Description:

I'm getting an error here trying to use a collection that conforms to my own protocol.

The error is `Cannot convert value of type 'SectionBlock.ItemCollection' to expected argument type '[_]'`

Here is the sample code:

protocol ItemModel: Hashable {}

protocol SectionModel: Hashable {
    associatedtype ItemCollection: Collection where ItemCollection.Element: ItemModel
    var elements: ItemCollection { get }
}

final class BlocksViewController<SectionBlock: SectionModel>: UIViewController {
    typealias ItemCollectionElement = SectionBlock.ItemCollection.Element
    
    private let initalBlocks: [SectionBlock]

    private lazy var dataSource: UICollectionViewDiffableDataSource<SectionBlock, ItemCollectionElement> = {
        return UICollectionViewDiffableDataSource(collectionView: UICollectionView(), cellProvider: { _, _, _ in
            return nil
        })
    }()
    
    func applyInitialData() {
        let snapshot = NSDiffableDataSourceSnapshot<SectionBlock, ItemCollectionElement>()
        snapshot.appendSections(initalBlocks)

        for section in initalBlocks {
            let items = section.elements
            snapshot.appendItems(items, toSection: section) //ERROR: Cannot convert value of type 'SectionBlock.ItemCollection' to expected argument type '[_]'
        }

        dataSource.apply(snapshot, animatingDifferences: false)
    }

    init(initalBlocks: [SectionBlock]) {
        self.initalBlocks = initalBlocks
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
@belkadan
Copy link
Contributor

The use of type sugar is probably throwing this off; it's saying "cannot convert value of type 'SectionBlock.ItemCollection' to expected argument type 'Array<_>'". That is, you haven't proven that items is an Array, just a Collection of some kind, but NSDiffableDataSourceSnapshot only works with Arrays. One option would be to explicitly construct an Array from your collection, Array(items).

@xedin, any thoughts on making this diagnostic clearer?

@xedin
Copy link
Member

xedin commented Jul 7, 2019

I'm starting to work toward porting more stuff related to argument conversions over to the new diagnostic framework, we should see some good improvements in the area soon.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

3 participants