22.02.2014 Views

Please, feel free to interrupt me at any time. - Stefan-Marr.de

Please, feel free to interrupt me at any time. - Stefan-Marr.de

Please, feel free to interrupt me at any time. - Stefan-Marr.de

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Please</strong>,<br />

<strong>feel</strong> <strong>free</strong> <strong>to</strong> <strong>interrupt</strong> <strong>me</strong> <strong>at</strong> <strong>any</strong> ti<strong>me</strong>.


Do you use:<br />

• Multiple Inheritance?<br />

• Mixins?


Traits<br />

A Language Fe<strong>at</strong>ure for PHP?<br />

<strong>Stefan</strong> <strong>Marr</strong><br />

PHP Unconference Hamburg<br />

26‐27 April 2008


Agenda<br />

1. Introduction <strong>to</strong> Traits<br />

2. Traits Applied<br />

3. Traits for PHP: Trait versus Graft<br />

4. Roundup and Conclusion<br />

4/26/2008 Traits: A Language Fe<strong>at</strong>ure for PHP?<br />

4


Which problem is <strong>to</strong> be solved?<br />

• Single inheritance<br />

• Simple but not<br />

expressive enough<br />

Vertebr<strong>at</strong>es<br />

• Leads <strong>to</strong><br />

• Co<strong>de</strong> duplic<strong>at</strong>ion<br />

• Inappropri<strong>at</strong>e<br />

hierarchies<br />

Dinosaurs<br />

Archaeoptrix<br />

fly()<br />

fly()<br />

Mammal<br />

B<strong>at</strong><br />

4/26/2008 1. Introduction <strong>to</strong> Traits<br />

5


Traits<br />

• Set of <strong>me</strong>thods<br />

• Possibly composed of other Traits<br />

• Composition is unor<strong>de</strong>red<br />

• Composition oper<strong>at</strong>ions<br />

– Sum<br />

– Exclusion<br />

– Aliasing<br />

• Can be fl<strong>at</strong>tened away<br />

TTrait<br />

TConflict<br />

TFoo<br />

TBar<br />

4/26/2008 1. Introduction <strong>to</strong> Traits 6


Whiteboard<br />

4/26/2008 7<br />

1. Introduction <strong>to</strong> Traits<br />

a()<br />

Base<br />

a()<br />

A<br />

^'B'<br />

^'A'<br />

a()<br />

T<br />

a()<br />

Base<br />

a()<br />

A<br />

^'B'<br />

^'A'<br />

^'T'<br />

a()<br />

Base<br />

A<br />

^'B'<br />

a()<br />

T<br />

a()<br />

Base<br />

a()<br />

A<br />

^'B'<br />

^'T'<br />

^'T'<br />

a()<br />

Base<br />

A<br />

^'B'<br />

a()<br />

T2<br />

a()<br />

Base<br />

a()<br />

A<br />

^'B'<br />

^'B'<br />

^'T2'<br />

a()<br />

T1<br />

^'T1'<br />

a()<br />

Base<br />

A<br />

^'B'<br />

a()<br />

T2<br />

a()<br />

Base<br />

a()<br />

A<br />

^'B'<br />

^'T1'<br />

^'T2'<br />

a()<br />

T1<br />

^'T1'<br />

!a<br />

a()<br />

Base<br />

A<br />

^'B'<br />

a()<br />

T2<br />

a()<br />

Base<br />

a()<br />

b()<br />

A<br />

^'B'<br />

^'B'<br />

^'T2'<br />

^'T2'<br />

a()<br />

T1<br />

^'T1'<br />

b→a


Case Studies Illustr<strong>at</strong>ing Benefits of Traits<br />

TRAITS APPLIED<br />

4/26/2008 <strong>Stefan</strong> <strong>Marr</strong> 8


Use Case for Traits in a PHP Library<br />

• ezcReflection enhances PHP Reflection API<br />

– Adds XDoclet like annot<strong>at</strong>ions for PHP<br />

– Enhances type hinting using PHPDoc type<br />

annot<strong>at</strong>ions<br />

• Illustr<strong>at</strong>es solution for co<strong>de</strong> duplic<strong>at</strong>ion<br />

problems<br />

4/26/2008 2. Traits Applied<br />

9


Refac<strong>to</strong>ring ezcReflection with Traits<br />

original<br />

with Traits<br />

green parts are extracted <strong>to</strong> the Traits on the right‐hand si<strong>de</strong>,<br />

and will be removed from the classes<br />

4/26/2008 2. Traits Applied<br />

10


Case Study: java.io [6]<br />

• M<strong>at</strong>ure co<strong>de</strong> base, in wi<strong>de</strong> use<br />

• java.io Library from Java 1.4.2<br />

• 79 classes, 25000 lines of co<strong>de</strong><br />

• Results<br />

– 12 classes changed<br />

– 30 duplic<strong>at</strong>ed <strong>me</strong>thods removed<br />

– 14 Traits introduces<br />

4/26/2008 2. Traits Applied<br />

11


Case Study:<br />

Smalltalk Collection Classes [10]<br />

• Problem: co<strong>de</strong> reuse vs. conceptual<br />

c<strong>at</strong>egoriz<strong>at</strong>ion<br />

– Unnecessary inheritance<br />

– Co<strong>de</strong> duplic<strong>at</strong>ion<br />

– Methods <strong>to</strong> high in hierarchy<br />

– Conceptual shortcomings<br />

• Results for core collection classes<br />

– 23 classes changed,<br />

12% less co<strong>de</strong>, 19.4% less <strong>me</strong>thods<br />

4/26/2008 2. Traits Applied<br />

13


Case Study:<br />

Smalltalk Collection Classes [10]<br />

• Lessons Learned<br />

– Traits simplify refac<strong>to</strong>ring<br />

– Very fine‐grained traits<br />

– Tools are important<br />

– Traits enable <strong>to</strong> <strong>de</strong>fer <strong>de</strong>sign of class hierarchy<br />

• Improve<strong>me</strong>nts<br />

– Uniformity, un<strong>de</strong>rstandability, reuse<br />

4/26/2008 2. Traits Applied<br />

14


Check for Limit<strong>at</strong>ions<br />

• Co<strong>de</strong> duplic<strong>at</strong>ion<br />

• Inappropri<strong>at</strong>e hierarchies<br />

• Diamond problem<br />

• Fragile hierarchies<br />

• Lack of composition control<br />

<br />

<br />

<br />

<br />

<br />

4/26/2008 2. Traits Applied<br />

15


Trait versus Graft<br />

TRAITS FOR PHP<br />

4/26/2008 <strong>Stefan</strong> <strong>Marr</strong>


Wh<strong>at</strong> is a Graft?<br />

• St<strong>at</strong>eful, non‐breakable entity of reuse<br />

– Derived from the Traits notion<br />

– Proposed with RFC: Non Breakable Traits for PHP<br />

• Why Graft?<br />

From The Grafter's Handbook by R.J. Garner<br />

4/26/2008 3. Traits for PHP: Trait versus Graft<br />

17


A Comparison<br />

Traits<br />

• Purpose, reuse of<br />

– behavior<br />

• Fe<strong>at</strong>ures<br />

– St<strong>at</strong>eless<br />

– Fl<strong>at</strong>tenable<br />

– Explicit conflict resolution<br />

– Highly composable<br />

• Notion<br />

– Language‐base<br />

Copy and Paste<br />

Grafts<br />

• Purpose, reuse of<br />

– modules smaller then classes<br />

• Fe<strong>at</strong>ures<br />

– St<strong>at</strong>eful<br />

– Hi<strong>de</strong>s complexity<br />

– Conflict avoidiance<br />

– Strong encapsul<strong>at</strong>ion<br />

• Notion<br />

– Deleg<strong>at</strong>ion with selfimperson<strong>at</strong>ion<br />

4/26/2008 3. Traits for PHP: Trait versus Graft<br />

18


Traits Syntax and Semantics<br />

trait A {<br />

public function small() { echo 'a'; }<br />

public function big() { echo 'A'; }<br />

}<br />

abstract public function doBFoo();<br />

public function doAFoo() {<br />

echo 'AFoo uses BFoo: ';<br />

$this‐>doBFoo();<br />

}<br />

trait B {<br />

public function small() { echo 'b'; }<br />

public function big() { echo 'B'; }<br />

}<br />

public function doBFoo() {<br />

echo 'B‐FOO';<br />

}<br />

class Talker {<br />

use A, B {<br />

B::small instead A::small,<br />

A::big instead B::big,<br />

B::big as talk<br />

}<br />

}<br />

$talker = new Talker();<br />

$talker‐>small(); // b<br />

$talker‐>big(); // A<br />

$talker‐>talk(); // B<br />

$talker‐>doAFoo(); // AFoo uses<br />

// BFoo: B‐FOO<br />

$talker‐>doBFoo(); // B‐FOO<br />

4/26/2008 3. Traits for PHP: Trait versus Graft<br />

19


Grafts Syntax and Semantics<br />

graft Counter {<br />

priv<strong>at</strong>e $cnt = 0;<br />

public function inc() {<br />

$this‐>cnt++;<br />

}<br />

public function reset() {<br />

$this‐>cnt = ‐1;<br />

$this‐>inc();<br />

}<br />

}<br />

graft DB {<br />

priv<strong>at</strong>e $db;<br />

public function connect() {<br />

$this‐>db = new FooDB('param');<br />

}<br />

public function reset() {<br />

$this‐>db‐>flush();<br />

$this‐>db = null;<br />

}<br />

public function doFoo(){echo 'foo';}<br />

}<br />

class MyPage {<br />

inclu<strong>de</strong> Counter {<br />

public incCnt() as inc();<br />

public resetCnt() as reset();<br />

}<br />

inclu<strong>de</strong> DB {<br />

public connect();<br />

public reset();<br />

}<br />

public function inc() {<br />

/* next page */<br />

}<br />

}<br />

$page = new MyPage();<br />

$page‐>connect();<br />

$page‐>incCnt(); //($Countercnt == 1)<br />

$page‐>resetCnt(); // $Countercnt = 0;<br />

$page‐>inc(); // go<strong>to</strong> next page<br />

$page‐>doFoo(); // FATAL ERROR<br />

4/26/2008 3. Traits for PHP: Trait versus Graft<br />

20


My Mental Image<br />

Traits<br />

Grafts<br />

MyClass<br />

MyClass<br />

4/26/2008 3. Traits for PHP: Trait versus Graft<br />

21


Trait Support, Discussion, Resu<strong>me</strong><br />

ROUNDUP AND CONCLUSION<br />

4/26/2008 <strong>Stefan</strong> <strong>Marr</strong>


Environ<strong>me</strong>nts Supporting Traits<br />

• Squeak: In standard distribution since 3.9 [11]<br />

Trait na<strong>me</strong>d: #TDrawing uses: {}<br />

draw<br />

ˆself drawOn: World canvas in: bounds<br />

bounds<br />

self require<strong>me</strong>nt<br />

Object subclass: #Circle<br />

instanceVariableNa<strong>me</strong>s: ’’<br />

traits: { TDrawing }<br />

...<br />

4/26/2008 4. Roundup and Conclusion<br />

23


Environ<strong>me</strong>nts Supporting Traits<br />

• Scala: st<strong>at</strong>ic‐typed language running on JVM [7]<br />

trait TDrawing {<br />

<strong>de</strong>f draw() : Any =<br />

drawOn(World.canvas(), bounds)<br />

<strong>de</strong>f bounds() : Any<br />

}<br />

class Circle extends TDrawing {<br />

...<br />

}<br />

4/26/2008 4. Roundup and Conclusion<br />

24


Environ<strong>me</strong>nts Supporting Traits<br />

• Perl6: Flavor of Traits with st<strong>at</strong>es called Roles [12]<br />

role TDrawing {<br />

<strong>me</strong>thod draw() {<br />

return drawOn(World.canvas(),<br />

bounds);<br />

}<br />

}<br />

class Circle does TDrawing {<br />

...<br />

}<br />

4/26/2008 4. Roundup and Conclusion<br />

25


Environ<strong>me</strong>nts Supporting Traits<br />

• Approaches for plain Java<br />

– Using AspectJ [2]<br />

– Utilizing an Eclipse Plugin [8]<br />

4/26/2008 4. Roundup and Conclusion<br />

26


Environ<strong>me</strong>nts Supporting Traits<br />

• Approaches for plain Java<br />

– Using AspectJ [2]<br />

– Utilizing an Eclipse Plugin [8]<br />

4/26/2008 4. Roundup and Conclusion<br />

27


Environ<strong>me</strong>nts Supporting Traits<br />

• C#: Pro<strong>to</strong>type Imple<strong>me</strong>nt<strong>at</strong>ion for Ro<strong>to</strong>r [9]<br />

trait TDrawing {<br />

public Object draw() {<br />

return drawOn(World.canvas(),<br />

this.bounds);<br />

}<br />

requires { public Object bounds(); }<br />

}<br />

class Circle {<br />

uses { TDrawing }<br />

...<br />

}<br />

4/26/2008 4. Roundup and Conclusion<br />

28


Resu<strong>me</strong><br />

• Traits are a appropri<strong>at</strong>e language construct <strong>to</strong>:<br />

– Achive conceptual consistent class hierarchies<br />

– Avoid co<strong>de</strong> duplic<strong>at</strong>ion<br />

– Enhance class composi<strong>to</strong>n capabilites<br />

• Available for different languages<br />

• Two different flavors proposed for PHP<br />

4/26/2008 4. Roundup and Conclusion<br />

29


Traits for PHP<br />

Request for Com<strong>me</strong>nts<br />

Version: 1.5<br />

D<strong>at</strong>e: 2008‐03‐06<br />

Author: <strong>Stefan</strong> <strong>Marr</strong><br />

Wiki: http://wiki.php.net/rfc/traits<br />

reST: http://www.stefan‐marr.<strong>de</strong>/rfc‐traits‐for‐php.txt<br />

HTML: http://www.stefan‐marr.<strong>de</strong>/artikel/rfc‐traits‐for‐php.html<br />

Non Breakable Traits for PHP<br />

Version: 1.0<br />

D<strong>at</strong>e: 2008‐02‐29<br />

Author: Joshua Thompson<br />

Wiki: http://wiki.php.net/rfc/nonbreakabletraits<br />

4/26/2008 4. Roundup and Conclusion<br />

30


Basic Liter<strong>at</strong>ure<br />

Main Article about Traits<br />

[3] S. DUCASSE, O. NIERSTRASZ, N. SCHÄRLI, R. WUYTS, AND A. P.<br />

BLACK, Traits: A Mechanism for Fine‐Grained Reuse, ACM<br />

Trans. Program. Lang. Syst., 28 (2006), pp. 331–388.<br />

Traits Extensions<br />

[1] A. BERGEL, S. DUCASSE, O. NIERSTRASZ, AND R. WUYTS, St<strong>at</strong>eful Traits and their<br />

Formaliz<strong>at</strong>ion, Journal of Computer Languages, Systems and Structures, 34<br />

(2007), pp. 83–108.<br />

[4] S. DUCASSE, R. WUYTS, A. BERGEL, AND O. NIERSTRASZ, User‐Changeable Visibility:<br />

Resolving Unanticip<strong>at</strong>ed Na<strong>me</strong> Clashes in Traits, SIGPLAN Not., 42 (2007),<br />

pp. 171–190.<br />

4/26/2008 4. Roundup and Conclusion<br />

31


Liter<strong>at</strong>ure<br />

[1]A. BERGEL, S. DUCASSE, O. NIERSTRASZ, AND R. WUYTS, St<strong>at</strong>eful Traits and their<br />

Formaliz<strong>at</strong>ion, Journal of Computer Languages, Systems and Structures, 34 (2007),<br />

pp. 83–108.<br />

[2]S. DENIER, Traits Programming with AspectJ, RSTI ‐ L'objet, 11 (2005), pp. 69–86.<br />

[3]S. DUCASSE, O. NIERSTRASZ, N. SCHÄRLI, R. WUYTS, AND A. P. BLACK, Traits: A Mechanism<br />

for Fine‐Grained Reuse, ACM Trans. Program. Lang. Syst., 28 (2006), pp. 331–388.<br />

[4]S. DUCASSE, R. WUYTS, A. BERGEL, AND O. NIERSTRASZ, User‐Changeable Visibility:<br />

Resolving Unanticip<strong>at</strong>ed Na<strong>me</strong> Clashes in Traits, SIGPLAN Not., 42 (2007), pp. 171–<br />

190.<br />

[5]S. MARR AND F. MENGE, ezcReflection, SVN Reposi<strong>to</strong>ry, eZ Components, January 2008.<br />

http://svn.ez.no/svn/ezcomponents/experi<strong>me</strong>ntal/Reflection.<br />

[6]E. R. MURPHY‐HILL, P. J. QUITSLUND, AND A. P. BLACK, Removing Duplic<strong>at</strong>ion from java.io:<br />

a Case Study using Traits, in OOPSLA '05: Companion <strong>to</strong> the 20th annual ACM<br />

SIGPLAN conference on Object‐oriented programming, systems, languages, and<br />

applic<strong>at</strong>ions, New York, NY, USA, 2005, ACM, pp. 282–291.<br />

4/26/2008 4. Roundup and Conclusion<br />

32


Liter<strong>at</strong>ure<br />

[7] PROGRAMMING METHODS LABORATORY, Traits for Scala, Programming<br />

Language, Ecole Polytechnique Fédérale <strong>de</strong> Lausanne, 2006.<br />

http://www.scala‐lang.org/intro/traits.html.<br />

[8] P. J. QUITSLUND, E. R. MURPHY‐HILL, AND A. P. BLACK, Supporting Java traits in<br />

Eclipse, in eclipse '04: Proceedings of the 2004 OOPSLA workshop on<br />

eclipse technology eXchange, New York, NY, USA, 2004, ACM, pp. 37–41.<br />

[9] S. REICHHART, Traits in C#. Vi<strong>de</strong>o of Talk <strong>at</strong> Microsoft Research, September<br />

2005.<br />

[10] N. SCHÄRLI, Traits — Composing Classes from Behavioral Building Blocks,<br />

PhD thesis, University of Berne, Feb. 2005.<br />

[11] SOFTWARE COMPOSITION GROUP, Traits for Squeak, Smalltalk VM, University<br />

of Berne, December 2006.<br />

http://www.iam.unibe.ch/ scg/Research/Traits/in<strong>de</strong>x.html.<br />

[12] L. WALL, Apocalypse 12: Class Composition with Roles, tech. report, The<br />

Perl Found<strong>at</strong>ion, 2004.<br />

http://www.perl.com/pub/a/2004/04/16/a12.html?page=11.<br />

4/26/2008 4. Roundup and Conclusion<br />

33


Any questions?<br />

Discussion<br />

•Wh<strong>at</strong> du you think?<br />

•Traits vs. Grafts


User‐Changeable Visibility and St<strong>at</strong>eful Traits<br />

TRAITS ENHANCED<br />

4/26/2008 <strong>Stefan</strong> <strong>Marr</strong>


User‐Changeable Visibility<br />

• Add flexibility <strong>to</strong> conflict handling<br />

• Introduces “Trait priv<strong>at</strong>e” <strong>me</strong>thods<br />

• Methods visibility changeable on composition<br />

4/26/2008 Addition: Traits Enhanced<br />

36


St<strong>at</strong>eless Traits<br />

• Problem: st<strong>at</strong>eless Traits are incomplete, not<br />

self‐contained units of reuse<br />

– Required st<strong>at</strong>e accessors blo<strong>at</strong> the interfaces<br />

– Encapsul<strong>at</strong>ion is viol<strong>at</strong>ed by public accessors<br />

– Reuse, composition is hampered<br />

– St<strong>at</strong>e access introduces fragility<br />

4/26/2008 Addition: Traits Enhanced<br />

37


St<strong>at</strong>eless Traits<br />

Squirrel<br />

nuts<br />

Scr<strong>at</strong><br />

getNuts()<br />

setNuts()<br />

R<strong>at</strong><br />

NutE<strong>at</strong>er<br />

collect()<br />

getNuts()<br />

setNuts()<br />

e<strong>at</strong>()<br />

WildBoar<br />

nuts<br />

getNuts()<br />

setNuts()<br />

nuts<br />

Hamster<br />

getNuts()<br />

setNuts()<br />

4/26/2008 Addition: Traits Enhanced<br />

38


St<strong>at</strong>eful Traits<br />

• Solution require<strong>me</strong>nts:<br />

– Preserver faltening property<br />

– Minimal and general approach<br />

– Language in<strong>de</strong>pen<strong>de</strong>d<br />

• Solution<br />

– St<strong>at</strong>e is priv<strong>at</strong>e <strong>to</strong> the Traits scope<br />

– Client can request access un<strong>de</strong>r new priv<strong>at</strong>e na<strong>me</strong><br />

– Client can <strong>me</strong>rge st<strong>at</strong>e<br />

4/26/2008 Addition: Traits Enhanced<br />

39


St<strong>at</strong>eful Traits<br />

Scr<strong>at</strong><br />

nuts<br />

NutE<strong>at</strong>er<br />

collect()<br />

e<strong>at</strong>()<br />

WildBoar<br />

Hamster<br />

Squirrel<br />

R<strong>at</strong><br />

4/26/2008 Addition: Traits Enhanced<br />

40

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!