Skip to content

Instantly share code, notes, and snippets.

@tarunon
Last active November 6, 2017 17:12
Show Gist options
  • Save tarunon/e09fe83094ae37efec7ed43c12a9076d to your computer and use it in GitHub Desktop.
Save tarunon/e09fe83094ae37efec7ed43c12a9076d to your computer and use it in GitHub Desktop.
import Foundation
// Make 5 pattern base types
class A {}
protocol B {}
protocol C: class {}
@objc protocol D {}
protocol E: NSObjectProtocol {}
// Make container protocol
protocol Container {
associatedtype Wrapped
}
// If Container conforms to Wrapped then return true.
func typeCheckShouldBeSuccess<T: Container>(_ type: T.Type) -> Bool {
return T.self is T.Wrapped.Type
}
// Always false because T.self is type and not conforms to value of Wrapped.
func typeCheckShouldBeFailure<T: Container>(_ type: T.Type) -> Bool {
return T.self is T.Wrapped
}
// Make Container implementation that extends of base type, and Wrapped is base type.
class ContainerA: A, Container {
typealias Wrapped = A
}
class ContainerB: B, Container {
typealias Wrapped = B
}
class ContainerC: C, Container {
typealias Wrapped = C
}
class ContainerD: D, Container {
typealias Wrapped = D
}
class ContainerE: NSObject, E, Container {
typealias Wrapped = E
}
// Case of A is fine.
let r01 = ContainerA.self is ContainerA.Wrapped.Type // true. ContainerA is subclass of A
let r02 = typeCheckShouldBeSuccess(ContainerA.self) // true. Should be same of r01
let r03 = ContainerA.self is ContainerA.Wrapped // false. ContainerA.self is Type, not value of A.
let r04 = typeCheckShouldBeFailure(ContainerA.self) // false. Should be same of r03
// Cases of B~E are (maybe) wrong.
let r05 = ContainerB.self is ContainerB.Wrapped.Type // true. ok.
let r06 = typeCheckShouldBeSuccess(ContainerB.self) // false. wrong. It should be same of r05.
let r07 = ContainerB.self is ContainerB.Wrapped // false. ok.
let r08 = typeCheckShouldBeFailure(ContainerB.self) // false. ok.
let r09 = ContainerC.self is ContainerC.Wrapped.Type // true. ok.
let r10 = typeCheckShouldBeSuccess(ContainerC.self) // false. wrong. It should be same of r09.
let r11 = ContainerC.self is ContainerC.Wrapped // false. ok.
let r12 = typeCheckShouldBeFailure(ContainerC.self) // true. wrong. It should be same of r11.
let r13 = ContainerD.self is ContainerD.Wrapped.Type // true. ok.
let r14 = typeCheckShouldBeSuccess(ContainerD.self) // false. wrong. It should be same of r13.
let r15 = ContainerD.self is ContainerD.Wrapped // true. wrong. ContainerD.self is Type, not value of D.
let r16 = typeCheckShouldBeFailure(ContainerD.self) // true. wrong.
let r17 = ContainerE.self is ContainerE.Wrapped.Type // true. ok.
let r18 = typeCheckShouldBeSuccess(ContainerE.self) // false. wrong. It should be same of r17.
let r19 = ContainerE.self is ContainerE.Wrapped // false. ok.
let r20 = typeCheckShouldBeFailure(ContainerE.self) // true. wrong. It should be same of r19.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment