desugaring
(Alex Semenikhine)
1
protocol ToTableRow {
associatedtype TableRow: IsTableRow
func toTableRow() -> TableRow
}
protocol IsTableRow {
associatedtype Model
}
// Removing the where clause will cause the code to no longer compile
class TableVM<Model: ToTableRow, TableRow: IsTableRow> where TableRow == Model.TableRow {
var model: [Model]
var tableRows: [TableRow]
init(model: [Model]) {
self.model = model
self.tableRows = model.map({ $0.toTableRow() })
}
}
Xcode tells me: Redundant conformance constraint 'TableRow': 'IsTableRow'
Reality: definitely not redundant :)
edit: I've come to realize it's telling me I can remove : IsTableRow from TableRow, because the where clause makes it redundant I suppose. I don't know that that's good however - because then it is no longer clear that TableRow is constrained in any way, without looking at the implementation of the ToTableRow protocol
Nevin
2
I’ve moved this topic to the Using Swift category, since it didn’t belong in Compiler Development.
• • •
Regarding your code, you don’t need two generic parameters on the class, only one:
class TableVM<Model: ToTableRow> {
var model: [Model]
var tableRows: [Model.TableRow]
⋮
}
If you want the shorthand of writing TableRow instead of Model.TableRow you can simply define a typealias in the class.
1 Like
desugaring
(Alex Semenikhine)
3
I was surprised to find your version compiling - it only works when there is a unique TableRow, for a Model.