Philipp Haller
June 01, 2017
220

# Quantifying and Explaining Immutability in Scala

June 01, 2017

## 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. 4
interface Map from JDK 8

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

6. Example
• Is the function sumOfAges referentially transparent?
def sumOfAges(p1: Person, p2: Person) =
p1.age + p2.age
6
“age” = getter
which does not mutate

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

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

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

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

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:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

30. 30

31. 31

32. 32