Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol inheritance issue

I try to set up various protocols that work hand in hand. Unfortunately I cannot make them work the way I want. Looking at the following code, I think my goal is obvious: I want to require a class that conforms to a protocol X. If it conforms to protocol Y instead but protocol Y inherits from protocol X, it should be accepted as a conforming class too. Instead I receive the following compile error

Unable to infer associated type 'VC' for protocol 'ViewModelType'

Inferred type 'ExampleViewControllerType' (by matching requirement 'viewController') is invalid: does not conform to 'ViewType'

Current setup:

protocol ViewModelType: class {
    associatedtype VC: ViewType
    weak var viewController: VC! { get set }
}

class ExampleViewModel: ViewModelType {
    weak var viewController: ExampleViewControllerType!
}

protocol ViewType: class { }    
protocol ExampleViewControllerType: ViewType { }

class ExampleViewController: UIViewController, ExampleViewControllerType { 

}
like image 293
Alexei S. Avatar asked Oct 19 '22 00:10

Alexei S.


1 Answers

I can see what you are getting at with the 'transitive' protocols, however your error is caused by your associatedtype declaration of VC as seen in the error.

Unable to infer associated type 'VC' for protocol 'ViewModelType'

I think the compiler is having difficulty here maybe because its an innapropriate use of the associatedtype declaration.

An associatedtype can be thought of as a placeholder for an unknown type.

By defining VC as an associatedtype you are letting any class that inherits ViewModelType decide what type VC should be.

In ExampleViewModel class you do this by setting the type using typealias in the conforming class.

Your viewController can then be an ExampleViewControllerType without causing the 'inferred' error

protocol ViewModelType: class {
    associatedtype VC
    var viewController: VC! { get set }
}

class ExampleViewModel: ViewModelType {
    typealias VC = ExampleViewControllerType
    weak var viewController: ExampleViewControllerType!
}

protocol ViewType: class { }
protocol ExampleViewControllerType: ViewType { }

class ExampleViewController: UIViewController, ExampleViewControllerType {

}
like image 72
Danoram Avatar answered Nov 03 '22 06:11

Danoram