Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

13 traits

on

  • 2,250 views

 

Statistics

Views

Total Views
2,250
Views on SlideShare
2,248
Embed Views
2

Actions

Likes
0
Downloads
15
Comments
0

1 Embed 2

http://www.orhowilearnedtolovetheperl.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Warning — toy example! Some real case study examples appear later.
  • Better would be: = aRectangle ^ self species = aRectangle species and: [self origin = aRectangle origin] and: [self corner = aRectangle corner]
  • NB: TRectangle only uses accessors to access state. The original Rectangle code may need to be adapted. Glue is needed to fulfil trait requirements, here implemented by accessors
  • See NSCollectionStream for a real example

13 traits 13 traits Presentation Transcript

  • 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 http://scg.unibe.ch/scgbib?query=stlit-traits
  • Roadmap
    • Why traits?
    • Traits in a Nutshell
    • Case study — Streams
    • Traits in Pharo
    • Future of Traits
    © Oscar Nierstrasz ST — Smalltalk Basics 2.
  • Roadmap
    • Why traits?
    • Traits in a Nutshell
    • Case study — Streams
    • Traits in Pharo
    • Future of Traits
    © Oscar Nierstrasz ST — Smalltalk Basics 2.
  • Problem: how to share behaviour across class hierarchies? © Oscar Nierstrasz Traits There are hundreds of methods we would like RectangleMorph to inherit from both Rectangle and Morph
  • 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
  • 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 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]
  • Mixins extend single inheritance with features that can be mixed into a class © Oscar Nierstrasz 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
  • Roadmap
    • Why traits?
    • Traits in a Nutshell
    • Case study — Streams
    • Traits in Pharo
    • Future of Traits
    © Oscar Nierstrasz ST — Smalltalk Basics 2.
  • 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 = aRectangle ^ self species = aRectangle species and: [self origin = aRectangle origin] and: [self corner = aRectangle corner]
  • Class = superclass + state + traits + glue © Oscar Nierstrasz Traits The class retains full control of the composition
  • Both traits and classes can be composed of traits © Oscar Nierstrasz Traits Trait named: #NSTPuttablePositionableStream uses: NSTPuttableStream + NSTPositionableStream category: 'Nile-Base-Traits' Object subclass: #NSTextStream uses: NSTPuttablePositionableStream + NSTCharacterWriting instanceVariableNames: 'collection position writeLimit readLimit' classVariableNames: '' poolDictionaries: '' category: 'Nile-Clients-TextStream'
  • Trait composition rules
    • Class methods take precedence over trait methods
    • Conflicts are resolved explicitly
    • Traits can be flattened away
    © Oscar Nierstrasz Traits
  • Class methods take precedence over trait methods © Oscar Nierstrasz Traits RectangleMorph>>printOn: prevails over Morph>>printOn:
  • Trait composition rules
    • Class methods take precedence over trait methods
    • Conflicts are resolved explicitly
    • Traits can be flattened away
    © Oscar Nierstrasz Traits
  • Conflicts are resolved explicitly © Oscar Nierstrasz Traits RectangleMorph subclass: #Morph uses: TRectangle @ {#rectanglePrintOn: -> #printOn:} – {#align:with: . #topRight . … } instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Morphic-TraitsDemo' Aliasing introduces an additional name for a method Exclusion removes a method from a trait
  • Trait composition rules
    • Class methods take precedence over trait methods
    • Conflicts are resolved explicitly
    • Traits can be flattened away
    © Oscar Nierstrasz Traits
  • Traits can be flattened away © Oscar Nierstrasz Traits A class using traits is equivalent to class that defines those traits locally
  • Roadmap
    • Why traits?
    • Traits in a Nutshell
    • Case study — Streams
    • Traits in Pharo
    • Future of Traits
    © Oscar Nierstrasz ST — Smalltalk Basics 2. Cassou, et al. Traits at Work: the design of a new trait-based stream library . JCLSS 2009.
  • The trouble with Streams © Oscar Nierstrasz Traits unused state methods too high copying canceling reimplementation
  • The Nile core © Oscar Nierstrasz Traits
  • Nile Stream classes © Oscar Nierstrasz Traits no methods too high no copying no unused state no reimplementation limited canceling
  • Other Nile Stream classes © Oscar Nierstrasz 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
  • Roadmap
    • Why traits?
    • Traits in a Nutshell
    • Case study — Streams
    • Traits in Pharo
    • Future of Traits
    © Oscar Nierstrasz ST — Smalltalk Basics 2.
  • 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
    © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 12.
  • Traits in Pharo 1.0 © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 12. Object subclass: # Behavior uses: TPureBehavior @ { #basicAddTraitSelector:withMethod: -> #addTraitSelector:withMethod: } instanceVariableNames: 'superclass methodDict format traitComposition localSelectors' classVariableNames: 'ObsoleteSubclasses' poolDictionaries: '' category: 'Kernel-Classes'
  • OmniBrowser supports trait browsing and navigation © Oscar Nierstrasz Traits navigation browsing required methods
  • Traits can be manipulated from the browser © Oscar Nierstrasz Traits
  • Traits and Classes share common behaviour Traits
  • Can classes share compiled methods from traits? © Oscar Nierstrasz Traits
    • Two problems:
    • super is statically bound
    • compiled methods know their class
    ⇒ methods are copied to method dictionaries when they are installed
  • Roadmap
    • Why traits?
    • Traits in a Nutshell
    • Case study — Streams
    • Traits in Pharo
    • Future of Traits
    © Oscar Nierstrasz ST — Smalltalk Basics 2.
  • 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
  • License
    • http://creativecommons.org/licenses/by-sa/3.0/
    © Oscar Nierstrasz Traits 1.
    • Attribution-ShareAlike 3.0 Unported
    • You are free:
      • to Share — to copy, distribute and transmit the work
      • to 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.