Navigation Menu

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-6977] NSObject inheritance causes: Error non-nominal type does not support explicit initialization #49525

Closed
swift-ci opened this issue Feb 10, 2018 · 7 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6977
Radar rdar://problem/37462507
Original Reporter ebg (JIRA User)
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, TypeChecker
Assignee @slavapestov
Priority Medium

md5: 4187fdb70abb1858fa5854feea50ed46

Issue Description:

I'm trying to instantiate a generic type which is constrained to be a protocol (which includes the init method), which is constrained be a concrete UIView subclass. However the init throws the compiler error: non-nominal type 'ViewType' does not support explicit initialization. This is only the case when we make Self: UIView. If we constrain Self to a non-NSObject, the code compiles.

import UIKit

protocol ListModel {
}

protocol ListView where Self: UIView {
    init(model: ListModel)
}

class ListController<ViewType>: UIViewController where ViewType: ListView {

    func update(listModels: [ListModel]) {
        let views = listModels.map { (model: ListModel) -> UIView in
            let view: UIView = ViewType(model: model) // error
            return view
        }
        /** ... add UIViews as subviews */
    }
} 

Expanded upon in the Swift forums:
https://forums.swift.org/t/error-non-nominal-type-does-not-support-explicit-initialization/9195

@belkadan
Copy link
Contributor

@swift-ci create

@slavapestov
Copy link
Member

Ooops, this is 'protocols inheriting from classes' and not what I thought it was.

@swift-ci
Copy link
Collaborator Author

Comment by Erich Graham (JIRA)

Any idea if this bug will be resolved in 4.2 or 5.0? This seems like a simple bug with bridging NSObjects, and is a pretty powerful use case for abstractions in UIKit code.

@slavapestov
Copy link
Member

I just checked and the test case now works in 4.2. However, note that writing "protocol ListView where Self: UIView" is not really supported in 4.2. In future releases, you might have to write "protocol ListView : UIView", instead.

@swift-ci
Copy link
Collaborator Author

Comment by Erich Graham (JIRA)

Thanks Slava!

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Jul 23, 2019

ebg (JIRA User), Could you verify if the problem is fixed and if so move the JIRA to "Closed"?

Thanks!
Anna

@swift-ci
Copy link
Collaborator Author

Comment by Erich Graham (JIRA)

Verified fixed

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants