Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Stateful traits in Pharo

346 views

Published on

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/].

Published in: Technology
  • Be the first to comment

Stateful traits in Pharo

  1. 1. Modular Stateful Traits in Pharo Pablo Tesone IMT - Lille-Douai / RMod INRIA
  2. 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. 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. 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. 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. 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. 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. 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. 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. 10. New Trait Composition Operations !10 • Alias Slot • Remove Slot • Merge Slots Adding Slots brings new conflicts.
  11. 11. !11 That’s not new!!! Kill him!!
  12. 12. Also Fix other problems !12 Pharo 6 Traits Monolithic Impl. Not Extensible Limited Polymorphic w/classes
  13. 13. Modular Implementation !13 • The kernel does not know nothing about traits. • They are loaded after. • Reducing Codintional Code.
  14. 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. 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. 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. 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. 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. 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. 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. 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. !22 Thanks!!! Conclusion • Stateful Traits! • Modular Solution (faster bootstrap, you can avoid it). • Extensible • Simpler MOP / Polymorphism with classes. • Same performance! • 100% Backward Compatible

×