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. Diamond of Variances
    Vu Nhat Minh / @orakaro
    try! Swift Tokyo 2018

    View full-size slide

  2. Mercari / Souzoh
    iOS / Swift Engineer / @orakaro

    View full-size slide

  3. What is Variance ?
    Subtyping relationship between type constructor
    Which relation of A and B make F a subtype of F ?

    View full-size slide

  4. Contravariance Covariance
    Invariance
    Phantom Variance
    A: B then F : F A: B then F : F
    A: B but no relation between F and F
    No relation between A and B but F : F
    If A is a subtype of B,
    then F is a subtype of F

    View full-size slide

  5. Swift Covariance: Array
    class Saiyan{}
    class SuperSaiyan: Saiyan{}
    let goku: Saiyan = SuperSaiyan()
    let saiyanArmy: [Saiyan] = [SuperSaiyan]()
    A: B
    [A] : [B]

    View full-size slide

  6. Swift Covariance: Optional
    class Saiyan{}
    class SuperSaiyan: Saiyan{}
    let gohan: SuperSaiyan? = SuperSaiyan()
    let sonOfGoku: Saiyan? = gohan
    A: B
    A? : B?

    View full-size slide

  7. Contravariance Covariance
    Invariance
    Phantom Variance
    A: B then F : F A: B then F : F
    A: B but no relation between F and F
    No relation between A and B but F : F
    If A is a subtype of B,
    then F is a subtype of F

    View full-size slide

  8. Swift Contravariance: Function Parameter
    class Saiyan{}
    class SuperSaiyan: Saiyan{}
    typealias Transform = (T) -> GreatApe
    let transformation: Transform = { _ in GreatApe() }
    let superTransformation: Transform = transformation
    A: B
    F : F

    View full-size slide

  9. Contravariance Covariance
    A: B then F : F A: B then F : F
    Covariance vs Contravariance
    typealias Transform1 = (T) -> Void // Contravariance
    typealias Transform2 = ((T) -> Void) -> Void // Covariance
    typealias Transform3 = (((T) -> Void) -> Void) -> Void // Contravariance
    vs

    View full-size slide

  10. Contravariance Covariance
    Invariance
    Phantom Variance
    A: B then F : F A: B then F : F
    A: B but no relation between F and F
    No relation between A and B but F : F

    View full-size slide

  11. Swift Invariance
    protocol Saiyan{}
    protocol SuperSaiyan: Saiyan{}
    class ZFighter {}
    let goku: ZFighter = ZFighter()
    let gokuSuperSaiyan: ZFighter = goku ❌
    // cannot convert value of type 'ZFighter' to specified type
    'ZFighter'
    Any custom defined type in Swift is Invariance
    Custom type

    View full-size slide

  12. Contravariance Covariance
    Invariance
    Phantom Variance
    A: B then F : F A: B then F : F
    A: B but no relation between F and F
    No relation between A and B but F : F

    View full-size slide

  13. Phantom Variance
    protocol Saiyan {}
    protocol Namekian {}
    typealias ZFighter<> = Void
    let fighter: ZFighter = ZFighter()
    A and B have no relation
    F : F

    View full-size slide

  14. Contravariance Covariance
    Invariance
    Phantom Variance
    A: B then F : F A: B then F : F
    A: B but no relation between F and F
    No relation between A and B but F : F

    View full-size slide

  15. Type of Variance
    • Covariance : Array and Optional
    • Contravariance : Function/Closure parameter
    • Invariance : Every Custom Type
    • Phantom Variance : Type parameter which is not used inside

    View full-size slide

  16. Twitter / Github: @orakaro
    Thank you

    View full-size slide