Reputation: 196
protocol TableRow {
func configure(with model: Any) // line a
}
protocol Row: TableRow {
associatedtype Model
func configure(with model: Model) // line b
func configure(with model: String) // line z
}
extension TableRow {
func configure(with model: Any) {
fatalError()
}
}
struct Product { }
struct ProductCell: Row {
typealias Model = Product // line c
func configure(with model: Product) { } // line d
}
Hello, hope my question make sense. The function is line a
and line b
looks like same. Is it ?
Even if i delete line c
and line d
compiler is okay. BUT if i added line z
compiler complain not conforming to add at line z
function. What i am missing ? Do i have to associatedtype String
under the associated Model ? Why ?
If different why compiler looks ok at below code
struct ProductCell: Row {}
You can test at your playground file
Upvotes: 0
Views: 39
Reputation: 535501
Okay, so this code is perfectly legal (with line z commented out):
protocol TableRow {
func configure(with model: Any) // line a
}
protocol Row: TableRow {
associatedtype Model
func configure(with model: Model) // line b
// func configure(with model: String) // line z
}
extension TableRow {
func configure(with model: Any) { // line c
fatalError()
}
}
struct Product { }
struct ProductCell: Row {
typealias Model = Product // line c
func configure(with model: Product) { } // line d
}
So what if we uncomment line z? Row is a protocol. That means it places demands on anyone who adopts it. So if you say
protocol Row: TableRow {
associatedtype Model
func configure(with model: Model) // line b
func configure(with model: String) // line z
}
...you are demanding that an adopter of Row implement two configure
methods, one which takes a type to be determined at compile time (the generic Model), the other which takes a String.
Your ProductCell claims to adopt Row, but it omits the second configure
method, so it doesn't conform properly. To make it conform in that case, you would need to echo precisely what the protocol requires:
struct ProductCell: Row {
typealias Model = Product // line c
func configure(with model: Product) { } // line d
func configure(with model: String) {}
}
Upvotes: 1