13 traits

35
13. Traits

Upload: the-world-of-smalltalk

Post on 20-May-2015

2.337 views

Category:

Education


1 download

TRANSCRIPT

Page 1: 13 traits

13. Traits

Page 2: 13 traits

Selected literature

> Cook. Interfaces and Specifications for the Smalltalk-80 Collection Classes. OOPSLA 1992

> Taivalsaari. On the Notion of Inheritance. ACM Computing Surveys, September 1996.

> Black, et al. Applying Traits to the Smalltalk Collection Hierarchy. OOPSLA 2003

> Ducasse, et al. Traits: A Mechanism for fine-grained Reuse. ACM TOPLAS, March 2006.

> Cassou, et al. Traits at Work: the design of a new trait-based stream library. JCLSS 2009

© Oscar Nierstrasz

Traits

2

http://scg.unibe.ch/scgbib?query=stlit-traits

Page 3: 13 traits

© Oscar Nierstrasz

ST — Smalltalk Basics

2.3

Roadmap

> Why traits?> Traits in a Nutshell> Case study — Streams> Traits in Pharo> Future of Traits

Page 4: 13 traits

© Oscar Nierstrasz

ST — Smalltalk Basics

2.4

Roadmap

> Why traits?> Traits in a Nutshell> Case study — Streams> Traits in Pharo> Future of Traits

Page 5: 13 traits

Problem: how to share behaviour across class hierarchies?

© Oscar Nierstrasz

Traits

5

There are hundreds of methods we would like RectangleMorph to inherit from both Rectangle and Morph

Page 6: 13 traits

The trouble with Single Inheritance

> Where to put the shared behaviour?— Sharing too high inappropriate methods must be “cancelled”⇒

> Duplicating code— Impacts maintenance

> Delegate— Ugly boilerplate delegation code

© Oscar Nierstrasz

Traits

6

Page 7: 13 traits

The trouble with Multiple Inheritance

> Conflicts must be resolved— Implicit resolution leads to

fragility when refactoring

> No unique super class— Must explicitly name super

methods to compose them

> Diamond problem— What to do about features

inherited along two paths?

© Oscar Nierstrasz

Traits

7

an IdentitySet(#topRight #align:with: #right: #leftCenter #bottom #center #height #right #topCenter #extent #bottomCenter #topLeft #width #printOn: #containsPoint: #left #top #intersects: #bottomLeft #bottom: #bottomRight #top: #left: #rightCenter)

an IdentitySet(#topRight #align:with: #right: #leftCenter #bottom #center #height #right #topCenter #extent #bottomCenter #topLeft #width #printOn: #containsPoint: #left #top #intersects: #bottomLeft #bottom: #bottomRight #top: #left: #rightCenter)

Rectangle selectors select: [:s | Morph selectors includes: s]Rectangle selectors select: [:s | Morph selectors includes: s]

Page 8: 13 traits

Mixins extend single inheritance with features that can be mixed into a class

© Oscar Nierstrasz

Traits

8

Page 9: 13 traits

The trouble with Mixins

> Mixins are composed linearly to resolve conflicts— Conflict resolution is sensitive to mixin composition order— Composing entity has no control!

> Fragile hierarchy— Changes may impact distant classes

© Oscar Nierstrasz

Traits

9

Page 10: 13 traits

© Oscar Nierstrasz

ST — Smalltalk Basics

2.10

Roadmap

> Why traits?> Traits in a Nutshell> Case study — Streams> Traits in Pharo> Future of Traits

Page 11: 13 traits

Traits are parameterized behaviours

> A trait— provides a set of methods— requires a set of methods— may be composed of other traits

> Traits do not specify any state!

© Oscar Nierstrasz

Traits

11

= aRectangle^ self species = aRectangle speciesand: [self origin = aRectangle origin]and: [self corner = aRectangle corner]

= aRectangle^ self species = aRectangle speciesand: [self origin = aRectangle origin]and: [self corner = aRectangle corner]

Page 12: 13 traits

Class = superclass + state + traits + glue

© Oscar Nierstrasz

Traits

12

The class retains full control of the composition

Page 13: 13 traits

Both traits and classes can be composed of traits

© Oscar Nierstrasz

Traits

13

Trait named: #NSTPuttablePositionableStreamuses: NSTPuttableStream + NSTPositionableStreamcategory: 'Nile-Base-Traits'

Trait named: #NSTPuttablePositionableStreamuses: NSTPuttableStream + NSTPositionableStreamcategory: 'Nile-Base-Traits'

Object subclass: #NSTextStreamuses: NSTPuttablePositionableStream + NSTCharacterWritinginstanceVariableNames: 'collection position writeLimit readLimit'classVariableNames: ''poolDictionaries: ''category: 'Nile-Clients-TextStream'

Object subclass: #NSTextStreamuses: NSTPuttablePositionableStream + NSTCharacterWritinginstanceVariableNames: 'collection position writeLimit readLimit'classVariableNames: ''poolDictionaries: ''category: 'Nile-Clients-TextStream'

Page 14: 13 traits

Trait composition rules

1. Class methods take precedence over trait methods2. Conflicts are resolved explicitly3. Traits can be flattened away

© Oscar Nierstrasz

Traits

14

Page 15: 13 traits

Class methods take precedence over trait methods

© Oscar Nierstrasz

Traits

15

RectangleMorph>>printOn: prevails over Morph>>printOn:

Page 16: 13 traits

Trait composition rules

1. Class methods take precedence over trait methods2. Conflicts are resolved explicitly3. Traits can be flattened away

© Oscar Nierstrasz

Traits

16

Page 17: 13 traits

Conflicts are resolved explicitly

© Oscar Nierstrasz

Traits

17

RectangleMorph subclass: #Morphuses: TRectangle @ {#rectanglePrintOn: -> #printOn:}

– {#align:with: . #topRight . … }instanceVariableNames: ''classVariableNames: ''poolDictionaries: ''category: 'Morphic-TraitsDemo'

RectangleMorph subclass: #Morphuses: TRectangle @ {#rectanglePrintOn: -> #printOn:}

– {#align:with: . #topRight . … }instanceVariableNames: ''classVariableNames: ''poolDictionaries: ''category: 'Morphic-TraitsDemo'

Aliasing introduces an additional name for a methodExclusion removes a method from a trait

Page 18: 13 traits

Trait composition rules

1. Class methods take precedence over trait methods2. Conflicts are resolved explicitly3. Traits can be flattened away

© Oscar Nierstrasz

Traits

18

Page 19: 13 traits

Traits can be flattened away

© Oscar Nierstrasz

Traits

19

A class using traits is equivalent to class that defines those traits locally

Page 20: 13 traits

© Oscar Nierstrasz

ST — Smalltalk Basics

2.20

Roadmap

> Why traits?> Traits in a Nutshell> Case study — Streams> Traits in Pharo> Future of Traits

Cassou, et al. Traits at Work: the design of a new trait-based stream library. JCLSS 2009.

Page 21: 13 traits

The trouble with Streams

© Oscar Nierstrasz

Traits

21

unused state

methods too high

copying

canceling

reimplementation

Page 22: 13 traits

The Nile core

© Oscar Nierstrasz

Traits

22

Page 23: 13 traits

Nile Stream classes

© Oscar Nierstrasz

Traits

23

no methods too highno copyingno unused stateno reimplementationlimited canceling

Page 24: 13 traits

Other Nile Stream classes

© Oscar Nierstrasz

Traits

24

Page 25: 13 traits

Assessment

> High reuse achieved— 40% less code in Stream hierarchy

> More general abstractions— Streams on any Collection— With equal or better performance

> Design traits around abstractions, not reuse— Avoid too fine-grained traits

> Traits or classes?— Prefer classes — use traits to resolve design conflicts

© Oscar Nierstrasz

Traits

25

Page 26: 13 traits

© Oscar Nierstrasz

ST — Smalltalk Basics

2.26

Roadmap

> Why traits?> Traits in a Nutshell> Case study — Streams> Traits in Pharo> Future of Traits

Page 27: 13 traits

© Oscar Nierstrasz

ST — Understanding Classes and Metaclasses

12.27

Traits in Pharo

> Language Extension— Extended the language kernel to represent traits— Modified the compilation process for classes

built from traits

> No changes to the VM— Essentially no runtime performance penalty— Except indirect instance variable access— But: This is common practice anyway

> No duplication of source code— Only byte-code duplication when installing methods

Page 28: 13 traits

© Oscar Nierstrasz

ST — Understanding Classes and Metaclasses

12.28

Traits in Pharo 1.0

Object subclass: #Behavioruses: TPureBehavior @

{ #basicAddTraitSelector:withMethod:-> #addTraitSelector:withMethod: }

instanceVariableNames: 'superclass methodDict formattraitComposition localSelectors'

classVariableNames: 'ObsoleteSubclasses'poolDictionaries: ''category: 'Kernel-Classes'

Page 29: 13 traits

OmniBrowser supports trait browsing and navigation

© Oscar Nierstrasz

Traits

29

navigation browsing required methods

Page 30: 13 traits

Traits can be manipulated from the browser

© Oscar Nierstrasz

Traits

30

Page 31: 13 traits

Traits and Classes share common behaviour

Traits

31

Page 32: 13 traits

Can classes share compiled methods from traits?

© Oscar Nierstrasz

Traits

32

Two problems:1.super is statically bound2.compiled methods know their class

⇒methods are copied to method dictionaries when they are installed

Page 33: 13 traits

© Oscar Nierstrasz

ST — Smalltalk Basics

2.33

Roadmap

> Why traits?> Traits in a Nutshell> Case study — Streams> Traits in Pharo> Future of Traits

Page 34: 13 traits

The future of Traits

> Stateful traits— some experimental solutions …

> Tool support— limited browser support in Pharo

> Automatic refactoring— some experiments with formal concept analysis

> Pure trait-based language— can traits and classes be unified?

> Traits in other languages— Perl, Scala, Fortress, …

© Oscar Nierstrasz

Traits

34

Page 35: 13 traits

© Oscar Nierstrasz

Traits

1.35

Attribution-ShareAlike 3.0 UnportedYou are free:

to Share — to copy, distribute and transmit the workto Remix — to adapt the work

Under the following conditions:Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license.

For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.

Any of the above conditions can be waived if you get permission from the copyright holder.Nothing in this license impairs or restricts the author's moral rights.

License

http://creativecommons.org/licenses/by-sa/3.0/