Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

4 interface Map from JDK 8

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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!

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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:)

Slide 11

Slide 11 text

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:

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Results (3) ScalaTest (v3.0.1) 21

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

30

Slide 31

Slide 31 text

31

Slide 32

Slide 32 text

32