https://developer.apple.com/documentation/swift/type(of:)#Finding-the-Dynamic-Type-in-a-Generic-Context
전달된 값의 dynamic type 을 반환한다.
<aside>
⌨️ Index
</aside>
Discussion
static type
dynamic type
- actual type at run-time
- can be a subtype of its concrete type
concrete type
- class, structure, enumeration, other non-protocol type
existential type
- protocol or protocol composition
예시 코드
- b 의 static type: A
- b 의 dynamic type: B
class A {}
class B: A{}
func printValue(_ value: A) {
let t = type(of: value)
print("\\(t)")
}
let b: A = B()
printValue(b)
// prints "B"
Finding the Dynamic Type in a Generic Context
특정 상황에서는 예상 하지 못한 결과가 나올 수도 있다.
- generic context with a type parameter bound to a protocol.
let stringAsP: P = "Hello!"