The diamond of Variances

5b42fb9892c0404457899919d583e087?s=47 Orakaro
February 28, 2018

The diamond of Variances

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

5b42fb9892c0404457899919d583e087?s=128

Orakaro

February 28, 2018
Tweet

Transcript

  1. 3.

    What is Variance ? Subtyping relationship between type constructor Which

    relation of A and B make F<A> a subtype of F<B> ?
  2. 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>
  3. 5.

    Swift Covariance: Array class Saiyan{} class SuperSaiyan: Saiyan{} let goku:

    Saiyan = SuperSaiyan() let saiyanArmy: [Saiyan] = [SuperSaiyan]() A: B [A] : [B]
  4. 6.

    Swift Covariance: Optional class Saiyan{} class SuperSaiyan: Saiyan{} let gohan:

    SuperSaiyan? = SuperSaiyan() let sonOfGoku: Saiyan? = gohan A: B A? : B?
  5. 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>
  6. 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>
  7. 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
  8. 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>
  9. 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 defined type in Swift is Invariance Custom type
  10. 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>
  11. 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>
  12. 14.
  13. 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>
  14. 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