Reputation: 2555
So I'm trying to understand generic protocols and classes:
protocol ListPresenterType where View.PDO.SW == Dispatcher.SW {
associatedtype Dispatcher: ListDispatcherType
associatedtype View: ListViewType
init(dispatcher: Dispatcher, state: @escaping (_ state: AppState)->(ListState<Dispatcher.SW>))
func attachView(_ view: View)
...
}
And I'm initiating it from another generic class:
class AbstractListViewController<Presenter: ListPresenterType, PDO: ListPDOCommonType, ...>: ListViewType, ... where PDO.SW == Presenter.Dispatcher.SW, ... {
func configure(withBla: bla) {
...
presenter = Presenter(dispatcher: dispatcher, state: state)
}
func someFunc() {
presenter.attachView(self) // ERROR: Cannot invoke 'attachView' with an argument list of type ...
}
As I understand, I'm trying to initialize a type conforming to a generic protocol, which works just fine, but the type of View must be inconsistent with what I'm trying to feed it in attachView(:)
.
Then I try to initialize it with concrete view, changing init
:
init(dispatcher: Dispatcher, view: View, state: @escaping (_ state: AppState)->(ListState<Dispatcher.SW>)) {
self.view = view
...
}
And in AbstractListViewController
:
presenter = Presenter(dispatcher: dispatcher, view: self, state: state)
And getting this infamous error:
Non-nominal type 'Presenter' does not support explicit initialization
Here're gists with relevant playgrounds:
attachView(:)
) https://gist.github.com/nikans/0fde838846ffa9ff2da48c923f850625Please notice that every empty protocol there is in fact a generic protocol, I've just removed the unnecessary details.
I'd like to understand:
Thanks.
Upvotes: 3
Views: 1654
Reputation: 2555
So it appears that in Xcode9 (beta 6) errors like Non-nominal type '%type' does not support explicit initialization
simply equals the mismatching types
error in less buggy Xcode (if that's a thing): cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'
Upvotes: 3