First Name: Pablo
Last Name: Tesone
Email: tesonep@gmail.com
Title: Stateful traits in Pharo
Abstract: This talk presents the new implementation of Stateful Traits included in Pharo 7.
We will include a brief description of the new implementation, and changes and the improvements compared with the old implementation.
Also, we will introduce how to use the included stateful support and the integration with complex slots.
Bio: Pablo Tesone is a PhD student at IMT Lille Douai, France.
He is studying Dynamic Software Update Solutions applied to Live programming environments, distributed systems and robotic applications.
He is interested in improving the tools and the daily development process.
He is an enthusiast of the object oriented programming and their tools.
He collaborates with different open source projects like the ones in the Pharo Community [http://pharo.org/] and the Uqbar Foundation [http://www.uqbar-project.org/].
2. Traits
• We have duplicated
behaviour.
• Present in different
hierarchies.
• Inheritance is not enough
!2
Person FileReference Operation
logModification
logCreation
...
Employee
logModification
logCreation
...
External
Resource logModification
logCreation
...
Sale
3. Traits (2)
• Reuse of behaviour
• Share behaviour outside the
hierarchy of classes.
• Reduce the duplication of
code.
• Manual resolution of conflicts.
• Not affecting the subclassing.
!3
logModification
logCreation
TAudit
<<uses>>
Employee
External
Resource
Sale
<<uses>>
<<uses>>
fullName
TNamed
<<uses>>
4. Some Implementation
Details
• Traits are flattened.
• Classes have copy of
the methods
• Handled by the traits
implementation… not
by the programmer.
• Transparent to the
Runtime
!4
...
Employee
...
logModification
logCreation
...
...
External
Resource
...
logModification
logCreation
...
...
Sale
...
logModification
logCreation
...
logModification
logCreation
TAudit
<<uses>>
<<uses>>
<<uses>>
5. But not perfect…
• Only can interact with
the instance through
messages.
• Needs of glue code
(usually accessors).
• Cannot define
instance variables
(slots)
!5
modification
creation
surname
name
...
Employee
modificationTime
modifiactionTime:
creationTime
creationTime:
surname
surname:
name
name:
modification
creation
...
External
Resource
modificationTime
modifiactionTime:
creationTime
creationTime:
...
modification
creation
...
Sale
modificationTime
modifiactionTime:
creationTime
creationTime:
...
logModification
logCreation
TAudit
<<uses>>
<<uses>>
<<uses>>
6. Stateful Traits
• Reuse of slots.
• Improving the
Initialisation
• We reduce the glue
code.
• Adding new operations
to handle conflicts.
!6
initializeTrait
logModification
TAudit
creation
modification
surname
name
...
Employee
surname
surname:
name
name:
...
External Resource
...
...
Sale
...
<<uses>>
<<uses>>
<<uses>>
8. Initialisation of instances
• Traited classes have
another initialisation
method.
• Perform the
initialisation of all the
traits.
• Generated if missed by
the traits mechanism.
!8
initializeTrait
logModification
TAudit
creation
modification
...
Employee
initialize
initializeTrait
initializeTAudit
initializeTWithRol
...
External Resource
initializeTrait
initialize
...
Sale
initializeTrait
initialize
<<uses>>
<<uses>>
<<uses>>
initializeTrait
rol
rol:
TWithRol
rol
<<uses>>
15. Solved by Polymorphism
!15
Object Object class
Metaclass
Employee
Employee
Class
Traited
Metaclass
TAudit
<<uses>>
Audit
classSide
TTraitedClass
<<uses>> <<uses>>
Class
Kernel
Traits
Library
16. Polymorphism w/Classes
• The traits are traited
classes…..
• But you cannot instantiate
them.
• We only provide the different
behaviour.
• Simplifies the tools
!16
Class Class class
Metaclass
Trait Trait Class
TAudit
:nil
TAudit
classSide
Metaclass
forTraits
TTraitedClass
TraitedMetaclass
17. Extensible Algebra
• We need the algebra to solve conflicts.
• New operations to handle slots:
• Rename
• Remove
• Alias
• Also the algebra easily extensible.
!17
Trait
Composition
Empty
Composition
Sequence
AliasMethod
.....
Trait
Composition
Remove
Slot
Rename
Slot
Merge
Slot
.....
18. Extensible Class Builder
• The class building process has been modified. Allowing
external extensions.
• All the creation of classes with traits are extension
methods.
!18
Shift
ClassBuilder
Default
Builder
Enhancer
compileMethodsFor: aBuilder
configureClass: newClass superclass: superclass
withLayoutType: layoutType slots: slots
afterMethodCompiled: aBuilder
TraitBuilderEnhancer
19. Does it scale?
• The methods and slots are flattened
• The slots are compiled in the target class.
• The initialisation code is chained.
• During runtime, there is no difference.
• 100 % Backward compatible.
• No VM Changes or needed support.
!19
20. Bonus: simplification of
MetaObject - Protocol
• Same MOP with classes.
• Classes, Traits (whatever) are only source of slots and methods.
• Better API for selectors, localSelectors and slots and localSlots.
• Simplifying the tools.
• System Traits deprecated (TClass, TBehavior, …)
• Still in progress…. so many tools to update.
!20
21. For the same price…. (?)
!21
Lines 22,606 15 %
Methods 2,897 21 %
Bootstrap
Process
5 min 30 %
Overall Pharo
Build
4 min 20 %
Kernel Reduction
Lines 6,557
Affected Packages 89
Tool Simplification
22. !22
Thanks!!!
Conclusion
• Stateful Traits!
• Modular Solution (faster bootstrap, you can
avoid it).
• Extensible
• Simpler MOP / Polymorphism with classes.
• Same performance!
• 100% Backward Compatible