Slideshow transcript
Slide 1: Please, feel free to interrupt me at any time.
Slide 2: Do you use: • Multiple Inheritance? • Mixins?
Slide 3: Traits A Language Feature for PHP? Stefan Marr PHP Unconference Hamburg 26-27 April 2008
Slide 4: Agenda 1. Introduction to Traits 2. Traits Applied 3. Traits for PHP: Trait versus Graft 4. Roundup and Conclusion
Slide 5: Which problem is to be solved? Vertebrates • Single inheritance • Simple but not expressive enough Dinosaurs Mammal • Leads to • Code duplication • Inappropriate Archaeoptrix Bat hierarchies fly() fly()
Slide 6: Traits Set of methods • Possibly composed of other Traits • Composition is unordered • TFoo foo() Composition operations • TTrait TBar m1() – Sum m2() bar() MyClass – Exclusion TConflict – Aliasing m2() • Can be flattened away
Slide 7: Whiteboard Base Base Base ^' B' ^' B' a() a() ^' B' a() Code Structure Code Structure Code Structure T1 ^' T1' a() A T A A T T2 a() ^' T' a() a() ^' A' ^' T' ^' T2' a() Base Base Base Compiled Result Compiled Result Compiled Result ^' B' ^' B' a() a() ^' B' a() A A A a() a() ^' T' ^' B' a() ^' A' Base Base ^' B' a() ^' B' a() Code Structure Code Structure T1 T1 ^' T1' a() ^' T1' a() A A T2 T2 b→a !a ^' T2' a() ^' T2' a() Base Base Compiled Result ^' B' a() Compiled Result ^' B' a() A A ^' B' a() a() ^' T1' ^' T2' b()
Slide 8: Case Studies Illustrating Benefits of Traits TRAITS APPLIED
Slide 9: Use Case for Traits in a PHP Library • ezcReflection enhances PHP Reflection API – Adds XDoclet like annotations for PHP – Enhances type hinting using PHPDoc type annotations • Illustrates solution for code duplication problems «interface» Reflector ReflectionProperty ReflectionClass ReflectionFunction ReflectionParameter ReflectionObject ReflectionMethod
Slide 10: Refactoring ezcReflection with Traits with Traits original green parts are extracted to the Traits on the right-hand side, and will be removed from the classes
Slide 11: Case Study: java.io [6] Mature code base, in wide use • java.io Library from Java 1.4.2 • 79 classes, 25000 lines of code • Results • – 12 classes changed – 30 duplicated methods removed – 14 Traits introduces
Slide 12: Seiten aus p282-murphy-hill from [6]
Slide 13: Case Study: Smalltalk Collection Classes [10] • Problem: code reuse vs. conceptual categorization – Unnecessary inheritance – Code duplication – Methods to high in hierarchy – Conceptual shortcomings • Results for core collection classes – 23 classes changed, 12% less code, 19.4% less methods
Slide 14: Case Study: Smalltalk Collection Classes [10] • Lessons Learned – Traits simplify refactoring – Very fine-grained traits – Tools are important – Traits enable to defer design of class hierarchy • Improvements – Uniformity, understandability, reuse
Slide 15: Check for Limitations Code duplication • Inappropriate hierarchies • Diamond problem • Fragile hierarchies • Lack of composition control •
Slide 16: Trait versus Graft TRAITS FOR PHP
Slide 17: What is a Graft? • Stateful, non-breakable entity of reuse – Derived from the Traits notion – Proposed with RFC: Non Breakable Traits for PHP grafting • Why Graft? From The Grafter's Handbook by R.J. Garner
Slide 18: A Comparison Traits Grafts • Purpose, reuse of • Purpose, reuse of – behavior – modules smaller then classes • Features • Features Stateless Stateful – – Flattenable Hides complexity – – Explicit conflict resolution Conflict avoidiance – – Highly composable Strong encapsulation – – • Notion • Notion – Language-base – Delegation with self- Copy and Paste impersonation
Slide 19: Traits Syntax and Semantics c l a s s Ta l k e r { t rai t A { p u b l i c f u n c t i o n s ma l l ( ) { e c h o u s e A, B { ' a' ; } B: : s ma l l i n s t e a d A: : s ma l l , publ i c f unc t i on bi g ( ) { ec ho A: : b i g i n s t e a d B: : b i g , ' A' ; } B: : b i g a s t a l k } a b s t r a c t p u b l i c f u n c t i o n d o BF o o ( ) ; } p u b l i c f u n c t i o n d o AF o o ( ) { e c h o ' AF o o u s e s BF o o : ' ; $t a l k e r = n e w Ta l k e r ( ) ; $ t h i s - >d o BF o o ( ) ; $t a l k e r - >s ma l l ( ) ; // b } $t a l k e r - >b i g ( ) ; // A } $t a l k e r - >t a l k ( ) ; // B t rai t B { $ t a l k e r - >d o AF o o ( ) ; // AF o o u s e s p u b l i c f u n c t i o n s ma l l ( ) { e c h o // BF o o : B- F OO ' b' ; } $ t a l k e r - >d o BF o o ( ) ; // B- F OO publ i c f unc t i on bi g ( ) { ec ho ' B' ; } p u b l i c f u n c t i o n d o BF o o ( ) { e c h o ' B- F OO' ; } }
Slide 20: Grafts Syntax and Semantics g r a f t Co u n t e r { c l a s s My Pa g e { pr i v a t e $c nt = 0; i n c l u d e Co u n t e r { publ i c f unc t i on i nc ( ) { p u b l i c i n c Cn t ( ) a s i n c ( ) ; $ t h i s - >c n t ++; p u b l i c r e s e t Cn t ( ) a s r e s e t ( ) ; } } publ i c f unc t i on r e s e t ( ) { i n c l u d e DB { $ t h i s - >c n t = - 1 ; publ i c c onne c t ( ) ; $ t h i s - >i n c ( ) ; publ i c r e s e t ( ) ; } } } publ i c f unc t i on i nc ( ) { g r a f t DB { /* n e x t p a g e * / pr i v a t e $db; } publ i c f unc t i on c onne c t ( ) { } $ t h i s - >d b = n e w F o o DB( ' p a r a m' ) ; } $ p a g e = n e w My Pa g e ( ) ; publ i c f unc t i on r e s e t ( ) { $ p a g e - >c o n n e c t ( ) ; $ t h i s - >d b - >f l u s h ( ) ; $ p a g e - >i n c Cn t ( ) ; //( $ Co u n t e r c n t == $ t h i s - >d b = n u l l ; 1) } $ p a g e - >r e s e t Cn t ( ) ; // $ Co u n t e r c n t = publ i c f unc t i on doFoo( ) {e c h o 0; ' f oo' ; } $ p a g e - >i n c ( ) ; // g o t o n e x t p a g e } $ p a g e - >d o F o o ( ) ; // F ATAL ERROR
Slide 21: My Mental Image Traits Grafts TraitA GraftA MyClass MyClass MyClass MyClass TraitB GraftB
Slide 22: Trait Support, Discussion, Resume ROUNDUP AND CONCLUSION
Slide 23: Environments Supporting Traits • Squeak: In standard distribution since 3.9 [11] Trait named: #TDrawing uses: {} draw ˆself drawOn: World canvas in: bounds bounds self requirement Object subclass: #Circle instanceVariableNames: ’’ traits: { TDrawing } ...
Slide 24: Environments Supporting Traits • Scala: static-typed language running on JVM [7] trait TDrawing { def draw() : Any = drawOn(World.canvas(), bounds) def bounds() : Any } class Circle extends TDrawing { ... }
Slide 25: Environments Supporting Traits • Perl6: Flavor of Traits with states called Roles [12] role TDrawing { method draw() { return drawOn(World.canvas(), bounds); } } class Circle does TDrawing { ... }
Slide 26: Environments Supporting Traits • Approaches for plain Java – Using AspectJ [2] – Utilizing an Eclipse Plugin [8]
Slide 27: Environments Supporting Traits • Approaches for plain Java – Using AspectJ [2] – Utilizing an Eclipse Plugin [8]
Slide 28: Environments Supporting Traits • C#: Prototype Implementation for Rotor [9] trait TDrawing { public Object draw() { return drawOn(World.canvas(), this.bounds); } requires { public Object bounds(); } } class Circle { uses { TDrawing } ... }
Slide 29: Resume • Traits are a appropriate language construct to: – Achive conceptual consistent class hierarchies – Avoid code duplication – Enhance class compositon capabilites • Available for different languages • Two different flavors proposed for PHP
Slide 30: Request for Comments Traits for PHP Version: 1.5 Date: 2008-03-06 Author: Stefan Marr Wiki: http://wiki.php.net/rfc/traits reST: http://www.stefan-marr.de/rfc-traits-for-php.txt HTML: http://www.stefan-marr.de/artikel/rfc-traits-for-php.html Non Breakable Traits for PHP Version: 1.0 Date: 2008-02-29 Author: Joshua Thompson Wiki: http://wiki.php.net/rfc/nonbreakabletraits
Slide 31: Basic Literature Main Article about Traits [3] S. DUCASSE, O. NIERSTRASZ, N. SCHÄRLI, R. WUYTS, AND A. P. BLACK, Traits: A Mechanism for Fine-Grained Reuse, ACM Trans. Program. Lang. Syst., 28 (2006), pp. 331–388. Traits Extensions [1] A. BERGEL, S. DUCASSE, O. NIERSTRASZ, AND R. WUYTS, Stateful Traits and their Formalization, Journal of Computer Languages, Systems and Structures, 34 (2007), pp. 83–108. [4] S. DUCASSE, R. WUYTS, A. BERGEL, AND O. NIERSTRASZ, User-Changeable Visibility: Resolving Unanticipated Name Clashes in Traits, SIGPLAN Not., 42 (2007), pp. 171–190.
Slide 32: Literature [1]A. BERGEL, S. DUCASSE, O. NIERSTRASZ, AND R. WUYTS, Stateful Traits and their Formalization, Journal of Computer Languages, Systems and Structures, 34 (2007), pp. 83–108. [2]S. DENIER, Traits Programming with AspectJ, RSTI - L'objet, 11 (2005), pp. 69–86. [3]S. DUCASSE, O. NIERSTRASZ, N. SCHÄRLI, R. WUYTS, AND A. P. BLACK, Traits: A Mechanism for Fine-Grained Reuse, ACM Trans. Program. Lang. Syst., 28 (2006), pp. 331–388. [4]S. DUCASSE, R. WUYTS, A. BERGEL, AND O. NIERSTRASZ, User-Changeable Visibility: Resolving Unanticipated Name Clashes in Traits, SIGPLAN Not., 42 (2007), pp. 171– 190. [5]S. MARR AND F. MENGE, ezcReflection, SVN Repository, eZ Components, January 2008. http://svn.ez.no/svn/ezcomponents/experimental/Reflection. [6]E. R. MURPHY-HILL, P. J. QUITSLUND, AND A. P. BLACK, Removing Duplication from java.io: a Case Study using Traits, in OOPSLA '05: Companion to the 20th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications, New York, NY, USA, 2005, ACM, pp. 282–291.
Slide 33: Literature [7] PROGRAMMING METHODS LABORATORY, Traits for Scala, Programming Language, Ecole Polytechnique Fédérale de Lausanne, 2006. http://www.scala-lang.org/intro/traits.html. [8] P. J. QUITSLUND, E. R. MURPHY-HILL, AND A. P. BLACK, Supporting Java traits in Eclipse, in eclipse '04: Proceedings of the 2004 OOPSLA workshop on eclipse technology eXchange, New York, NY, USA, 2004, ACM, pp. 37– 41. [9] S. REICHHART, Traits in C#. Video of Talk at Microsoft Research, September 2005. [10] N. SCHÄRLI, Traits — Composing Classes from Behavioral Building Blocks, PhD thesis, University of Berne, Feb. 2005. [11] SOFTWARE COMPOSITION GROUP, Traits for Squeak, Smalltalk VM, University of Berne, December 2006. http://www.iam.unibe.ch/ scg/Research/Traits/index.html. [12] L. WALL, Apocalypse 12: Class Composition with Roles, tech. report, The Perl Foundation, 2004. http://www.perl.com/pub/a/2004/04/16/a12.html?page=11.
Slide 34: Any questions? Discussion •What du you think? •Traits vs. Grafts
Slide 35: User-Changeable Visibility and Stateful Traits TRAITS ENHANCED
Slide 36: User-Changeable Visibility • Add flexibility to conflict handling • Introduces “Trait private” methods • Methods visibility changeable on composition Walker +walk() hide getExtremity +getExtremity() : Legs Penguin Swimmer +move() +swim() +getExtremity() : Wings
Slide 37: Stateless Traits • Problem: stateless Traits are incomplete, not self-contained units of reuse – Required state accessors bloat the interfaces – Encapsulation is violated by public accessors – Reuse, composition is hampered – State access introduces fragility
Slide 38: Stateless Traits WildBoar NutEater nuts getNuts() Scrat collect() setNuts() nuts getNuts() setNuts() getNuts() Hamster eat() setNuts() nuts getNuts() setNuts() Squirrel Rat
Slide 39: Stateful Traits • Solution requirements: – Preserver faltening property – Minimal and general approach – Language independed • Solution – State is private to the Traits scope – Client can request access under new private name – Client can merge state
Slide 40: Stateful Traits WildBoar NutEater nuts Scrat collect() eat() Hamster Squirrel Rat





Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 0 (more)