I had a problem, I spent 2 days refactoring my code, and suddenly I realized that I had made a huge mistake that Swift does not allow me to assign a specific type to a protocol that uses associated type such as:
protocol CollectionViewDataManager {
associatedtype DataType: Hashable
}
var dataManager: CollectionViewDataManager<Int> { // Protocol 'CollectionViewDataManager' can only be used as a generic constraint because it has Self or associated type requirements
// Returns a different dataManager based on a different situation.
}
It's just so weird to me that CollectionViewDataManager needs an associatedtype and I give it a specific type, but why doesn't Swift allow it?
From my point of view, I don't think there is any difference between:
protocol CollectionViewDataManager {
associatedtype DataType: Hashable
}
var dataManager: CollectionViewDataManager<Int>
and
protocol CollectionViewDataManager {
typealias DataType = Int
}
var dataManager: CollectionViewDataManager
and
struct CollectionViewDataManager<DataType>{
}
var dataManager: CollectionViewDataManager<Int>
Sadly, this just isn't working, but why? I understand why can't I simply return a protocol has associated type directly, but why can't I assign a specific type to it?