Software Composition Paradigms: Sommersemester 2015
Software Composition Paradigms: Sommersemester 2015
Sommersemester 2015
Radu Muschevici
Software Engineering Group, Department of Computer Science
2015-05-05
1 / 44
Beyond Inheritance:
Mixins & Traits
2 / 44
Mixins
3 / 44
Mixins
A mixin is a uniform extension of many different parent classes with
the same set of elds and methods.
[Ancona, Lagorio, and Zucca 2000]
A class may inherit some or all if its functionality from one or more
mixins via multiple inheritance.
class Insect
class Cicada extends Insect with Singer
class Bird extends Singer with Flyer
// given Flyer as another trait
Static trait
inheritance (builds
new class)
class Person {
def tell {
println("here's a little story ...")
Dynamic trait
}
inheritance
}
(anonymous class)
val singingPerson = new Person with Singer
singingPerson.sing
5 / 44
Mixins in Scala
Simple Example
trait Similarity {
def isSimilar(x: Any): Boolean
def isNotSimilar(x: Any): Boolean = !isSimilar(x)
}
isSimilar is abstract
8 / 44
9 / 44
trait Ordered[A] {
def compare(that: A): Int
def < (that: A): Boolean = (this compare that) > 0
def > (that: A): Boolean = (this compare that) < 0
def <= (that: A): Boolean = (this compare that) <= 0
def >= (that: A): Boolean = (this compare that) >= 0
def compareTo(that: A): Int = compare(that)
}
11 / 44
12 / 44
An Implementation
import scala.collection.mutable.ArrayBuffer
class BasicQueue extends IntQueue {
private val buf = new ArrayBuffer[Int]
def get() = buf.remove(0)
def put(x : Int) { buf += x }
}
13 / 44
A Modication Trait
14 / 44
When several methods are applicable for a given call, the one
dened on the most specic class or trait, according to the
linearisation, is selected.
16 / 44
17 / 44
Scalas Linearisation
2. Starting with the rightmost parent type and working left, compute
the linearisation of each type, appending its linearisation to the
cumulative linearisation. (Ignore AnyRef and Any for now.)
3. Working from left to right, remove any type if it appears again to
the right of the current position.
18 / 44
Scala Linearisation
Scalas Linearisation: Example
classAnimal
class Animal
traitFurryextendsAnimal
trait Furry extends Animal
traitHasLegsextendsAnimal
trait HasLegs extends Animal
traitFourLeggedextendsHasLegs
trait FourLegged extends HasLegs
classCatextendsAnimalwithFurrywithFourLegged
AnyRef
Animal
HasLegs
Furry
FourLegged
Cat
19 / 44
Scala Linearisation
Any
Inheritance
Linearisation
AnyRef
Animal
HasLegs
Furry
FourLegged
Cat
20 / 44
Any
Inheritance
Linearisation
AnyRef
21 / 44
22 / 44
24 / 44
25 / 44
Traits
26 / 44
Traits: Motivation
Mixins pose numerous problems for reuse, e.g. little control over
composition.
27 / 44
Traits
A trait is a set of methods divorced from any class hierarchy.
[Ducasse et al. 2006]
28 / 44
Traits
A trait is a set of methods divorced from any class hierarchy.
[Ducasse et al. 2006]
29 / 44
ovided
ethods
Provided
methods
Fig. 10.
TReadStream
on:
collection
atEnd
collection:
atStart
position
setToEnd
position:
setToStart
nextPosition
maxPosition
minPosition
next
Required
methods
Required
methods
[Ducasse
et methods
al. 2006]
The trait TReadStream with provided and
required
Stphane Ducasse, Oscar Nierstrasz, Nathanael Schrli, Roel Wuyts, Andrew P. Black: Tra
mechanism for fine-grained reuse. ACM Trans. Program. Lang. Syst. 28(2): 331-388 (200
81
30 / 44
Trait Composition
31 / 44
Trait Operations
32 / 44
Class
Class
Trait
Trait
Fig. 11.
21
ReadStream
initialize
collection
collection:
position
position:
TReadStream
on:
collection
atEnd
collection:
atStart
position
setToEnd
position:
setToStart
nextPosition
maxPosition
minPosition
next
fulfilled by
Saturday 29 September 12
Running Example. Suppose that we want to implement a library that provides streams
which may be readable, writeable, both readable and writeable, or synchronized. For clarity, trait names start with the letter T, and class names do not. We italicize required meth-33 / 44
84
34 / 44
Flattening
In any class dened using traits, the traits can be inlined to obtain an
equivalent class denition that does not use traits.
Traits can be compiled away.
35 / 44
Flattening
Flattening: Example
aturday 29 September 12
fulfilled by
23
fulfilled by
TReadStream
atEnd
collection
position
TWriteStream
nextPut:
atEnd
on:
collection
position
TPositionableStream
atEnd
collection
atStart
collection:
setToEnd
position
setToStart
position:
nextPosition
maxPosition
minPosition
TPositionableStream
atEnd
collection
atStart
collection:
setToEnd
position
setToStart
position:
nextPosition
maxPosition
minPosition
next
on:
flattened to
TReadStream
on:
collection
atEnd
collection:
atStart
position
setToEnd
position:
setToStart
nextPosition
maxPosition
minPosition
next
TWriteStream
on:
collection
atEnd
collection:
atStart
position
setToEnd
position:
setToStart
nextPosition
maxPosition
minPosition
nextPut:
86
36 / 44
Conict Resolution
Conicts
A conict arises when composing two traits that provide identically
named methods with different bodies.
37 / 44
fulfilled by
next
25
fulfilled by
ReadWriteStream
collection:
collection
position
position:
TSyncReadStream
readNext
acquireLock
releaseLock
TSynchronize
acquireLock
semaphore
releaseLock
semaphore:
initialize
TReadStream
on:
collection
atEnd
collection:
atStart
position
setToEnd
position:
setToStart
nextPosition
maxPosition
minPosition
next
readNext
next
on:
conflict
TReadStream
collection
position
TPositionableStream
atEnd
collection
atStart
collection:
setToEnd
position
setToStart
position:
nextPosition
maxPosition
minPosition
TWriteStream
collection
nextPut:
position
on:
TPositionableStream
atEnd
collection
atStart
collection:
setToEnd
position
setToStart
position:
nextPosition
maxPosition
minPosition
87
38 / 44
Circle
initialize
=
hash
rgb
rgb:
center
center:
radius
radius:
drawOn:
TColor
red
green
~=
=
hash
rgb
rgb:
TColor
TCircle
=
hash
...
bounds
area
center
center:
radius
radius:
red
green
~=
=
hash
rgb
rgb:
TCircle
TDrawing
draw
bounds
refresh
drawOn:
refreshOn:
=
hash
...
bounds
area
center
center:
radius
radius:
TDrawing
draw
bounds
refresh
drawOn:
refreshOn:
39 / 44
Traits
1. Purely units of reuse
2. Composition by sum, overriding, aliasing and exclusion: More
ne-grained control
3. Several traits can be applied to a class in a single operation.
4. Composition is unordered & requires explicit resolution of conicts.
5. Inheritance is separate from trait composition and only reects
conceptual hierarchies.
40 / 44
Stateful Traits
(next lesson)
41 / 44
Download link:
http://dl.acm.org/citation.cfm?id=97982
42 / 44
References I
Ancona, Davide, Giovanni Lagorio, and Elena Zucca (2000). Jam A
Smooth Extension of Java with Mixins. In: European Conference
on Object-Oriented Programming. Vol. 1850. LNCS. Springer,
pp. 154178.
Barrett, Kim, Bob Cassels, Paul Haahr, David A. Moon, Keith Playford,
and P. Tucker Withington (1996). A Monotonic Superclass
Linearization for Dylan. In: ACM Conference on Object-Oriented
Programming Systems, Languages, and Applications. OOPSLA 96.
ACM Press, pp. 6982.
Bracha, Gilad and William Cook (1990). Mixin-based inheritance. In:
ACM Conference on Object-Oriented Programming Systems,
Languages, and Applications. OOPSLA/ECOOP 90. ACM Press,
pp. 303311.
43 / 44
References II
Ducasse, Stphane, Oscar Nierstrasz, Nathanael Schrli, Roel Wuyts,
and Andrew P. Black (2006). Traits: A Mechanism for Fine-grained
Reuse. In: ACM Transactions on Programming Languages and
Systems 28.2, pp. 331388.
Odersky, Martin, Lex Spoon, and Bill Venners (2011). Programming in
Scala: A Comprehensive Step-by-Step Guide. 2nd. USA: Artima
Incorporation.
44 / 44