Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The diamond of Variances

Orakaro
February 28, 2018

The diamond of Variances

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

Orakaro

February 28, 2018
Tweet

More Decks by Orakaro

Other Decks in Technology

Transcript

  1. What is Variance ? Subtyping relationship between type constructor Which

    relation of A and B make F<A> a subtype of F<B> ?
  2. 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. Swift Covariance: Array class Saiyan{} class SuperSaiyan: Saiyan{} let goku:

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

    SuperSaiyan? = SuperSaiyan() let sonOfGoku: Saiyan? = gohan A: B A? : B?
  5. 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. 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. 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. 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. 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. 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. 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. 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. Type of Variance • Covariance : Array and Optional •

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