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

Quantifying and Explaining Immutability in Scala

Philipp Haller
June 01, 2017
360

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
  2. 2

  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
  4. 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
  5. 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!
  6. 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
  7. 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
  8. 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
  9. 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:)
  10. 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:
  11. 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
  12. Remainder of the Talk • Empirical results evaluating immutability in

    open source code bases • Empirical results evaluating causes for mutability of type definitions 13
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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..
  20. 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
  21. More Details • Philipp Haller, Ludvig Axelsson. Quantifying and Explaining

    Immutability in Scala. PLACES@ETAPS 2017: 21-27 • Ludvig Axelsson. Immutability: An Empirical Study in Scala.
 Master’s thesis. KTH Royal Institute of Technology, Stockholm, Sweden. forthcoming 28
  22. 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
  23. 30

  24. 31

  25. 32