protocol Identifies : Hashable {}
// Something is Identifiable if it has an Identifier
protocol Identifiable : Hashable {
typealias Identifier : Identifies
var identifier : Identifier { get }
}
extension String : Identifies {}
class Person : Identifiable {
var identifier : String = "avoid init for now"
}
protocol Service {
typealias Object : Identifiable
func lookup (id : Object.Identifier) -> Object?
}
class PersonService <P:Person> : Service { // crashes here...
func lookup(id: P.Identifier) -> P? {
return nil
}
}The crash is avoidable with
P:Person where P:IdentifiableWhy?
Also, assume I want to avoid all type inferencing of the protocol typealiases. How would I modify the code to be as explicit as possible regarding type relationships?