630

# The diamond of Variances

try! Swift Tokyo 2018 presentation.
https://www.tryswift.co/events/2018/tokyo/en/#diamond

#### Orakaro

February 28, 2018

## Transcript

Tokyo 2018

3. ### What is Variance ? Subtyping relationship between type constructor Which

relation of A and B make F<A> a subtype of F<B> ?
4. ### Contravariance Covariance Invariance Phantom Variance A: B then F<B> :

F<A> A: B then F<A> : F<B> A: B but no relation between F<A> and F<B> No relation between A and B but F<A> : F<B> If A is a subtype of B, then F<A> is a subtype of F<B>
5. ### Swift Covariance: Array class Saiyan{} class SuperSaiyan: Saiyan{} let goku:

Saiyan = SuperSaiyan() let saiyanArmy: [Saiyan] = [SuperSaiyan]() A: B [A] : [B]
6. ### Swift Covariance: Optional class Saiyan{} class SuperSaiyan: Saiyan{} let gohan:

SuperSaiyan? = SuperSaiyan() let sonOfGoku: Saiyan? = gohan A: B A? : B?
7. ### Contravariance Covariance Invariance Phantom Variance A: B then F<B> :

F<A> A: B then F<A> : F<B> A: B but no relation between F<A> and F<B> No relation between A and B but F<A> : F<B> If A is a subtype of B, then F<B> is a subtype of F<A>
8. ### Swift Contravariance: Function Parameter class Saiyan{} class SuperSaiyan: Saiyan{} typealias

Transform<T> = (T) -> GreatApe let transformation: Transform<Saiyan> = { _ in GreatApe() } let superTransformation: Transform<SuperSaiyan> = transformation A: B F<B> : F<A>
9. ### Contravariance Covariance A: B then F<B> : F<A> A: B

then F<A> : F<B> Covariance vs Contravariance typealias Transform1<T> = (T) -> Void // Contravariance typealias Transform2<T> = ((T) -> Void) -> Void // Covariance typealias Transform3<T> = (((T) -> Void) -> Void) -> Void // Contravariance vs
10. ### Contravariance Covariance Invariance Phantom Variance A: B then F<B> :

F<A> A: B then F<A> : F<B> A: B but no relation between F<A> and F<B> No relation between A and B but F<A> : F<B>
11. ### Swift Invariance protocol Saiyan{} protocol SuperSaiyan: Saiyan{} class ZFighter<T> {}

let goku: ZFighter<Saiyan> = ZFighter() let gokuSuperSaiyan: ZFighter<SuperSaiyan> = goku ❌ // cannot convert value of type 'ZFighter<Saiyan>' to specified type 'ZFighter<SuperSaiyan>' Any custom deﬁned type in Swift is Invariance Custom type
12. ### Contravariance Covariance Invariance Phantom Variance A: B then F<B> :

F<A> A: B then F<A> : F<B> A: B but no relation between F<A> and F<B> No relation between A and B but F<A> : F<B>
13. ### Phantom Variance protocol Saiyan {} protocol Namekian {} typealias ZFighter<>

= Void let fighter: ZFighter<Saiyan> = ZFighter<Namekian>() A and B have no relation F<A> : F<B>

15. ### Contravariance Covariance Invariance Phantom Variance A: B then F<B> :

F<A> A: B then F<A> : F<B> A: B but no relation between F<A> and F<B> No relation between A and B but F<A> : F<B>
16. ### Type of Variance • Covariance : Array and Optional •

Contravariance : Function/Closure parameter • Invariance : Every Custom Type • Phantom Variance : Type parameter which is not used inside