SlideShare a Scribd company logo
1 of 22
Download to read offline
Modular Stateful
Traits in Pharo
Pablo Tesone
IMT - Lille-Douai / RMod INRIA
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
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>>
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>>
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>>
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>>
Stateful Traits (2)
• Flattened Methods.

• Flattened Slots.

• Everything is compiled
correctly.

• Methods defined in a
trait accesses slots
defined in it directly.
!7
initializeTrait
logModification
TAudit
creation
modification
...
creation
modification
...
Employee
...
initializeTrait
logModification
...
...
creation
modification
...
External Resource
...
initializeTrait
logModification
...
...
creation
modification
...
Sale
...
initializeTrait
logModification
...
<<uses>>
<<uses>>
<<uses>>
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>>
Initialisation of instances
!9
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>>
Sale >> initialize
super initialize.
self initializeTrait
Employee >> initialize
super initialize.
self initializeTrait
Employee >> initializeTrait
self initializeTWithRol.
self initializeTAudit
New Trait Composition
Operations
!10
• Alias Slot

• Remove Slot

• Merge Slots
Adding Slots brings new
conflicts.
!11
That’s not
new!!! Kill him!!
Also Fix other problems
!12
Pharo 6 Traits
Monolithic Impl.
Not Extensible
Limited Polymorphic w/classes
Modular Implementation
!13
• The kernel does not know nothing about
traits.

• They are loaded after.

• Reducing Codintional Code.
Modular Implementation
!14
Behavior >> includesBehavior: aClass
self isTrait ifTrue: [ ^false ].
^self == aClass or:[self inheritsFrom: aClass]
Behavior >> includesBehavior: aClass
^self == aClass or:[self inheritsFrom: aClass]
Trait >> includesBehavior: aClass
^false
Pharo 6
Pharo 7
Solved by Polymorphism
!15
Object Object class
Metaclass
Employee
Employee
Class
Traited
Metaclass
TAudit
<<uses>>
Audit
classSide
TTraitedClass
<<uses>> <<uses>>
Class
Kernel
Traits
Library
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
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
.....
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
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
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
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
Thanks!!!
Conclusion
• Stateful Traits!

• Modular Solution (faster bootstrap, you can
avoid it).

• Extensible

• Simpler MOP / Polymorphism with classes.

• Same performance!

• 100% Backward Compatible

More Related Content

Similar to Stateful traits in Pharo

Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Yasuko Ohba
 
Java class 1
Java class 1Java class 1
Java class 1
Edureka!
 

Similar to Stateful traits in Pharo (20)

C,s&s
C,s&sC,s&s
C,s&s
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Advanced Reflection in Pharo
Advanced Reflection in PharoAdvanced Reflection in Pharo
Advanced Reflection in Pharo
 
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
Pragmatic Patterns of Ruby on Rails - Ruby Kaigi2009
 
Abusing Java Remote Interfaces
Abusing Java Remote InterfacesAbusing Java Remote Interfaces
Abusing Java Remote Interfaces
 
Java class 1
Java class 1Java class 1
Java class 1
 
Rails Tips and Best Practices
Rails Tips and Best PracticesRails Tips and Best Practices
Rails Tips and Best Practices
 
Introduction to ML.NET
Introduction to ML.NETIntroduction to ML.NET
Introduction to ML.NET
 
Ruby On Rails Pitfalls
Ruby On Rails PitfallsRuby On Rails Pitfalls
Ruby On Rails Pitfalls
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
c++ Unit I.pptx
c++ Unit I.pptxc++ Unit I.pptx
c++ Unit I.pptx
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
C++ Online Training
C++ Online TrainingC++ Online Training
C++ Online Training
 
An Introduction to SPL, the Standard PHP Library
An Introduction to SPL, the Standard PHP LibraryAn Introduction to SPL, the Standard PHP Library
An Introduction to SPL, the Standard PHP Library
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
JavaTutorials.ppt
JavaTutorials.pptJavaTutorials.ppt
JavaTutorials.ppt
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Herding a Cat with Antlers - Catalyst 5.80
Herding a Cat with Antlers - Catalyst 5.80Herding a Cat with Antlers - Catalyst 5.80
Herding a Cat with Antlers - Catalyst 5.80
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 

More from ESUG

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
ESUG
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
ESUG
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
ESUG
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
ESUG
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
ESUG
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
ESUG
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
ESUG
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
ESUG
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
ESUG
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
ESUG
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
ESUG
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
ESUG
 

More from ESUG (20)

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
 
Technical documentation support in Pharo
Technical documentation support in PharoTechnical documentation support in Pharo
Technical documentation support in Pharo
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
 
Sequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in Pharo
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector Tuning
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing Score
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design Mooc
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Stateful traits in Pharo

  • 1. Modular Stateful Traits in Pharo Pablo Tesone IMT - Lille-Douai / RMod INRIA
  • 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>>
  • 7. Stateful Traits (2) • Flattened Methods. • Flattened Slots. • Everything is compiled correctly. • Methods defined in a trait accesses slots defined in it directly. !7 initializeTrait logModification TAudit creation modification ... creation modification ... Employee ... initializeTrait logModification ... ... creation modification ... External Resource ... initializeTrait logModification ... ... creation modification ... Sale ... initializeTrait logModification ... <<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>>
  • 9. Initialisation of instances !9 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>> Sale >> initialize super initialize. self initializeTrait Employee >> initialize super initialize. self initializeTrait Employee >> initializeTrait self initializeTWithRol. self initializeTAudit
  • 10. New Trait Composition Operations !10 • Alias Slot • Remove Slot • Merge Slots Adding Slots brings new conflicts.
  • 12. Also Fix other problems !12 Pharo 6 Traits Monolithic Impl. Not Extensible Limited Polymorphic w/classes
  • 13. Modular Implementation !13 • The kernel does not know nothing about traits. • They are loaded after. • Reducing Codintional Code.
  • 14. Modular Implementation !14 Behavior >> includesBehavior: aClass self isTrait ifTrue: [ ^false ]. ^self == aClass or:[self inheritsFrom: aClass] Behavior >> includesBehavior: aClass ^self == aClass or:[self inheritsFrom: aClass] Trait >> includesBehavior: aClass ^false Pharo 6 Pharo 7
  • 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