Please help me understand this section!
I'm reading Finding the Dynamic Type in a Generic Context that has this snippet:
func printGenericInfo<T>(_ value: T) {
let t = type(of: value)
print("'\(value)' of type '\(t)'")
}
protocol P {}
extension String: P {}
let stringAsP: P = "Hello!"
printGenericInfo(stringAsP)
// 'Hello!' of type 'P'
... that's followed up by this sentence:
This unexpected result occurs because the call to
type(of: value)insideprintGenericInfo(_:)must return a metatype that is an instance ofT.Type, but String.self (the expected dynamic type) is not an instance of P.Type (the concrete metatype of value).
1. How come String.self is not an instance of P when I can run this code?
func f(_ t: P.Type) { print("...") }
f(String.self)
2. Why does type(of:) return the concrete metatype outside but not inside generic functions?
print("'\(stringAsP)' of type '\(type(of: stringAsP))'")
// 'Hello!' of type 'String'