3 ms·
I don't know Swift, but the problem seems to be that you can't do extension ListItem: Hashable where ListItem is a protocol. That's a little irksome, sure
by Veedrac 11y ago
I don't know Swift, but the problem seems to be that you can't do
extension ListItem: Hashable
where ListItem is a protocol. That's a little irksome, sure, but why not just do
struct ListItemBox {
var item: ListItem
}
func ==(lhs: ListItemBox, rhs: ListItemBox) -> Bool {
return lhs.item.id == rhs.item.id
}
extension ListItemBox: Hashable {
var hashValue: Int {
return item.id
}
}
var stuff: Set<ListItemBox>;
Of course, you should be very careful about cross-type equality here. But that's no easier with any other model either.
- seanalltogether 11y agoI think it can be reduced into simpler terms in fact. In swift you simply can't do var stuff:Set var stuff:Set<Hashable> var stuff:Set<MyProtocol> Sets don't allow for mixed base types, and that's what a protocol would allow. All objects in a set must contain a similar base type. This is actually true in obj-c as well where everything in a set had to derive from NSObject.