In this sample code, Self.self
doesn't behave as expected. Depending on the type of the parameter chosen, the identifier property has not the same value.
protocol Identifiable : class {
static var identifier: String { get }
}
extension Identifiable {
static var identifier: String {
return String(describing: Self.self)
}
}
class Animal : Identifiable {}
class Tiger : Animal {}
Animal.identifier // Animal
Tiger.identifier // Tiger
func identifiableIdentifier<T: Identifiable>(of type: T.Type) -> String {
type // Tiger.Type
return type.identifier
}
identifiableIdentifier(of: Tiger.self) // Animal
func animalIdentifier<T: Animal>(for type: T.Type) -> String {
type // Tiger.Type
return type.identifier
}
animalIdentifier(for: Tiger.self) // Tiger
Does someone know why ?