Scala Diamond
Tomer Ben David
Got your fiance a diamond?
Let’s hope she is not a software
developer
Because if she is...
You should think of another trick
Fortunately, there is a way out
Did you tell her about scala diamond
linearization?
What is it you ask?
It’s our way of getting you out of
trouble
Just think about it
The diamond problem is due to
single parent having multiple
children
We call it linearization
What if we made sure every single
parent had only one child?
No multiple children for single
parents..
No diamond problem
So we only need to figure out how to
do this linearization
Well its simple follow these simple
steps
B extends A
C extends A
D extends B with C
We start with D we have a single
type D all is fine
Do you see an “extends” or “with”?
Yes, then expand every type with its
supertypes and get rid of the main
type you will have now...
Getting rid of the main type
(recursion we work on D) (D) we get
B with C
linearize B
(Any with AnyRef with A with B)
Linearize C
(Any with AnyRef with A with C)
All together now:
(Any with AnyRef with A with B) with
(Any with AnyRef with A with C) with
D
Remove duplications from LEFT
→ (Any with AnyRef with A with B)
with (Any with AnyRef with A with C)
with D
What do we have (from RIGHT!):
Any with AnyRef with A with B with
with C with D ←
So methods first from D then from C
then from B … you get the idea