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

Quantifying and Explaining Immutability in Scala

Philipp Haller
June 01, 2017
220

Quantifying and Explaining Immutability in Scala

Philipp Haller

June 01, 2017
Tweet

Transcript

  1. Quantifying and Explaining
    Immutability in Scala
    Philipp Haller
    KTH Royal Institute of Technology
    Stockholm, Sweden
    Joint work with Ludvig Axelsson
    1

    View Slide

  2. 2

    View Slide

  3. Why Immutability in Scala?
    • Scala is a powerful functional language
    • Functional programming emphasizes programming
    with…
    • first-class functions and
    • immutable data structures
    • Concurrency safety
    • Fault tolerance in distributed systems
    3

    View Slide

  4. 4
    interface Map from JDK 8

    View Slide

  5. Example: Referential Transparency
    • An expression is referentially transparent if it can be
    replaced with its corresponding value without changing
    the program’s behavior (Mitchell 2002)
    • A function is referentially transparent if its application to
    the same value evaluates to the same result
    5

    View Slide

  6. Example
    • A simple read-only function:
    • Is the function sumOfAges referentially transparent?
    def sumOfAges(p1: Person, p2: Person) =
    p1.age + p2.age
    6
    “age” = getter
    which does not mutate
    its receiver!

    View Slide

  7. Example
    class Person(val name: String, var age: Int)
    val john = new Person(“John”, 30)
    val judy = new Person(“Judy”, 55)
    val sum1 = sumOfAges(john, judy)
    john.age += 1 // birthday
    val sum2 = sumOfAges(john, judy)
    // sum1 != sum2
    Referential transparency violated
    Passing identical
    objects!
    7

    View Slide

  8. Immutability in Scala
    What do we have?
    • Predefined value types (Int, Boolean, Double, etc.)
    • val members, lazy val members
    • Simple case classes are immutable by default
    – No declared or inherited var members
    – All field types immutable
    • Value classes wrapping immutable types
    8

    View Slide

  9. Examples
    • Immutable case class:
    • Immutable value class:
    9
    case class Person(name: String, age: Int)
    trait Printable extends Any {
    def print(): Unit = println(this)
    }
    class Meter(val value: Double) extends AnyVal with Printable {
    def +(m: Meter): Meter = new Meter(value + m.value)
    }
    Universal trait

    View Slide

  10. Type Immutability in Scala?
    Attempt using type classes:
    10
    trait Immutable[T]
    def sendMessage[T: Immutable](msg: T): Unit
    def sendMessage[T](msg: T)(implicit ev: Immutable[T]): Unit
    Context bound
    (The latter expands to:)

    View Slide

  11. Type Immutability in Scala?
    Issues:
    • Have to introduce implicit values manually
    • Have to trust manual definitions
    11
    implicit val wrong =
    new Immutable[collection.mutable.Set[Int]] {}
    Example:

    View Slide

  12. Immutability in Scala
    • Scala enables creating immutable types
    • However:
    – Cannot express immutability as a requirement of types
    – No statistics on the use of immutability in Scala
    12

    View Slide

  13. Remainder of the Talk
    • Empirical results evaluating immutability in open source
    code bases
    • Empirical results evaluating causes for mutability of type
    definitions
    13

    View Slide

  14. Notions of Immutability
    • We consider immutability of type definitions
    • Others consider reference immutability
    – Does a reference enable mutating an object?
    • Three different immutability properties:
    – Deep immutability
    – Shallow immutability
    – Conditional deep immutability
    14
    Fields
    not reassignable
    but may refer to
    mutable objects

    View Slide

  15. Questions
    • Question 1: how frequent is each immutability property
    for classes, traits, and objects?
    • Question 2: for classes/traits/objects that are not deeply
    immutable: what are the most common reasons why
    stronger immutability properties are not satisfied?
    15

    View Slide

  16. Immutability Analysis
    • Compiler plugin for Scala 2.11.x
    • For each template maintain value from immutability
    lattice: { bot, deep, shallow, mut }
    • Implementation based on Reactive Async which provides
    lattice-based variables with cyclic dependency resolution
    – See my talk at Scala Days Berlin 2016
    16
    https://github.com/luax/scala-immutability-plugin

    View Slide

  17. Empirical Study
    • Analyzed open source projects:
    • Scala standard library (v2.11.8), 33107 SLOC1)
    • Akka actor package (v2.4.17), 16910 SLOC
    • ScalaTest (v3.0.1), 39452 SLOC
    • Signal/Collect distributed graph processing system
    (v8.0.2), 10159 SLOC
    • More results (not shown) for Scala.js and Scalafmt
    17
    1) Measured using cloc v1.6

    View Slide

  18. Question 1
    How frequent is each immutability property for classes,
    traits, and objects?
    18

    View Slide

  19. Results (1)
    Scala standard library (v2.11.8)
    19
    Must be zero!

    View Slide

  20. Results (2)
    Akka actor package (v2.4.17)
    20

    View Slide

  21. Results (3)
    ScalaTest (v3.0.1)
    21

    View Slide

  22. Results (4)
    Signal/Collect (v8.0.2)
    22

    View Slide

  23. Question 1: Summary
    • Majority of type definitions in analyzed projects is
    immutable
    – That is, satisfies one of the immutability properties
    • Deep immutability is often conditional: depends on
    instantiation of type parameters or abstract types
    • Low percentage of mutable case classes
    • Very low percentage of mutable singleton objects
    23

    View Slide

  24. Question 2
    For classes/traits/objects that are not deeply immutable:
    what are the most common reasons why stronger
    immutability properties are not satisfied?
    24

    View Slide

  25. Attribute Table
    25
    Source unavailable
    E.g., Java type

    View Slide

  26. Causes of Mutability
    26
    3 %
    7 %
    10 %
    80 %
    Parent type mutable Reassignable field (private)
    Parent type unknown Reassignable field (public)
    Parent type
    unknown
    Reassignable
    field (private)
    Parent type
    mutable
    Scala std library (v2.11.8)
    Looking only
    at mutable
    templates..

    View Slide

  27. Scala std library (v2.11.8)
    Causes of Shallow Immutability
    27
    4 %
    5 %
    33 %
    24 %
    34 %
    Parent type shallow immutable
    val field with unknown type
    val field with mutable type
    val field with shallow immutable type
    Other
    val field
    with mutable
    type
    Parent
    type shallow
    immutable
    val field
    with unknown
    type

    View Slide

  28. More Details
    • Philipp Haller, Ludvig Axelsson. Quantifying and Explaining
    Immutability in Scala. [email protected] 2017: 21-27
    • Ludvig Axelsson. Immutability: An Empirical Study in Scala.

    Master’s thesis. KTH Royal Institute of Technology, Stockholm,
    Sweden. forthcoming
    28

    View Slide

  29. Conclusion
    • Even with simple notions of immutability:

    Majority of type definitions in popular open source
    projects is immutable
    – Statistics for case classes and non-case classes very
    different
    – Very low percentage of mutable singleton objects
    • Is it time for more support from the language?
    29

    View Slide

  30. 30

    View Slide

  31. 31

    View Slide

  32. 32

    View Slide