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

Variance & Types - Scalabase

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Variance & Types - Scalabase

What is Variance, the connection between Types & Variance and how Scala supports it?
Deep dive into types of Variances and how they relate to Functions.

Avatar for Asjad Baig

Asjad Baig

May 14, 2021
Tweet

Other Decks in Programming

Transcript

  1. Will discuss... • Type Bounds • How to use ::

    Variance and its type • Liskov Substitution Principle • Function trait connection with Variance • How compiler ensures Variance
  2. Type Bounds 1. A & B both are types 2.

    A .: B A subtype of B 3. A .: B B subtype of A / A supertype of B
  3. Lower Bounds • [A .: Bulldog] • Range of possibilities

    A can have ? • {Dog, Animal, Any, Bulldog}
  4. Upper Bounds • [A .: Dog] • Range of possibilities

    A can have ? • {Dobermann, Greyhound, Bulldog, Nothing, Dog}
  5. Multi Bounds • [Animal .: A .: Bulldog] • Range

    of possibilities A can have ? • {Animal, Dog, Bulldog}
  6. Variance • Scala supports subtyping • Variance refers to how

    subtyping between more complex types relates to subtyping between their components
  7. CoVariance • C[A] .: C[B] • The subtyping relation is

    preserved in the same direction • Parameterize as C[+ T] (Type Notation) • Most Immutable Scala Collections are Covariant
  8. ContraVariance • C[A] .> C[B] • The subtyping relation is

    in the opposite direction • Parameterize as C[- T]
  9. Modelling ZooKeeper • class ZooKeeper[-T] • Is there a way

    to restrict it to just Animals ? • class ZooKeeper[-T .: Animal]
  10. InVariance • C[A] .= C[B] • There’s no relation with

    the parameterized type • Parameterize as C[ T] • Mutable collections are Invariant
  11. How it started • Let functionQ(x) be a property provable

    about objects x of type B . • Then functionQ(y) should be provable for objects y of type A where A .: B
  12. How it started If A .: B, then everything one

    can to do with a value of type B one should also be able to do with a value of type A. How it’s going • Let functionQ(x) be a property provable about objects x of type B . • Then functionQ(y) should be provable for objects y of type A where A .: B
  13. If A .: B, then everything one can to do

    with a value of type B one should also be able to do with a value of type A. How it’s going
  14. How it started • Let functionQ(x) be a property provable

    about objects x of type B . • Then functionQ(y) should be provable for objects y of type A where A .: B
  15. Function Typing Rule • If we have functions ◦ fn1

    = A1 .> B1 ◦ fn2 = A2 .> B2 • A1 .> B1 .: A2 .> B2 (i.e fn1 .: fn2) • Only when :: ◦ B1 .: B2 Covariant in Result Types ◦ A1 .: A2 Contravariant in Argument types
  16. Function for Elephant • Elephant eats Grass • fn1 ::

    Animal .> Napier • fn2 :: Elephant .> Grass
  17. Function Trait • How N input function should look ?

    • FunctionN[ -Input1,..,-InputN, +Result] • But there’s a limitation here with value N
  18. Function Trait • How N input function should look ?

    • FunctionN[ -Input1,..,-InputN, +Result] • But there’s a limitation here with value N • N = 22
  19. Function Trait • How N input function should look ?

    • FunctionN[ -Input1,..,-InputN, +Result] • But there’s a limitation here with value N • N = 22 • But, in scala3 this limitation is no more!!
  20. Scala Compiler Checks • Covariant type params can only appear

    in method results • Contravariant type param can only appear in method param • Invariant type param can appear anywhere
  21. Inference 1. Scala immutable collection, Producer => Covariant 2. Consumer

    modelling => Contravariant 3. Mutable Collections => Invariant 4. Thanks to Bounds => i.e. add() operation for Covariant types List 5. FunctionN [ -Input1,..,-InputN , +Result] (scala3)
  22. Resources 1. FP Principles EPFL : Coursera 2. Foundations of

    FP : Julien Truffaut 3. RockTheJVM 4. DevInsideYou