13. Traits. Selected literature  Cook. Interfaces and Specifications for the Smalltalk-80 Collection Classes. OOPSLA 1992  Taivalsaari. On the Notion.

Slides:



Advertisements
Similar presentations
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
Advertisements

Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz.
The State Transition Diagram
Win8 on Intel Programming Course Desktop : Sensors Cédric Andreolli Intel Software
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
12. Common Errors, a few Puzzles. © O. Nierstrasz P2 — Common Errors, a few Puzzles 12.2 Common Errors, a few Puzzles Sources  Cay Horstmann, Computing.
EngageNY.org ©2012 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
ESE Einführung in Software Engineering 6. Modeling Objects and Classes Prof. O. Nierstrasz.
Introduction to Software Engineering 7. Modeling Behaviour.
The Branching Storyboard Chapter 4.3 in Sketching the User Interface: The Workbook Image from:
S.Ducasse 1 Metaclasses in 7 Steps Classes are objects too... Classes are instances of other classes... One model applied twice.
OORPT Object-Oriented Reengineering Patterns and Techniques 7. Problem Detection Prof. O. Nierstrasz.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
“Traits: Composable Units of Behavior” N. Schaerli, S. Ducasse, O. Nierstrasz, and A. Black, U Bern / OHSU, ECOOP 2003 “Traits: A Mechanism for Fine-grained.
Object-Oriented Reengineering Patterns and Techniques Prof. O. Nierstrasz Prof. S. Ducasse T.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
7. Understanding Classes and Metaclasses. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed.
ESE Einführung in Software Engineering N. XXX Prof. O. Nierstrasz Fall Semester 2009.
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
The Software Composition Group Prof. O. Nierstrasz
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
Metamodeling Seminar X. CHAPTER Prof. O. Nierstrasz Spring Semester 2008.
12. Traits and Classboxes (and Changeboxes). © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 12.2 Roadmap  Problems with Inheritance 
© Oscar Nierstrasz ST — Smalltalk Basics 2.1 Change sets  Make sure your changes are logged to a new change set.
ESE Einführung in Software Engineering X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
N. XXX Prof. O. Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.
9. Understanding Classes and Metaclasses. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify your metamodel — A fully reflective system models.
7. Fixed Points. © O. Nierstrasz PS — Fixed Points 7.2 Roadmap  Representing Numbers  Recursion and the Fixed-Point Combinator  The typed lambda calculus.
12. Summary, Trends, Research. © O. Nierstrasz PS — Summary, Trends, Research Roadmap  Summary: —Trends in programming paradigms  Research:...
ESE Einführung in Software Engineering 6. Modeling Objects and Classes Prof. O. Nierstrasz.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
12. Traits and Classboxes. © Oscar Nierstrasz ST — Traits and Classboxes 12.2 Roadmap  Problems with Inheritance  Traits  Refactoring Collections with.
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 MakeMoney Corp. C*O of MakeMoney Corp. Our Vision  We invest in software  We do not know software 
OORPT Object-Oriented Reengineering Patterns and Techniques X. CHAPTER Prof. O. Nierstrasz.
CP — Concurrent Programming X. CHAPTER Prof. O. Nierstrasz Wintersemester 2005 / 2006.
12. eToys. © O. Nierstrasz PS — eToys 12.2 Denotational Semantics Overview:  … References:  …
SWOT Analysis Strengths Weaknesses SWOT Opportunities Threats.
Collecting Images & Clippings Chapter 2.3 in Sketching User Experiences: The Workbook.
Win8 on Intel Programming Course The challenge Paul Guermonprez Intel Software
Multicore Programming
What is a sketch? Chapter 1.2 addendum Sketching User Experiences: The Workbook.
© Copyright Showeet.com I NSERT YOUR TITLE HERE. © Copyright Showeet.com Insert Your Title Here 2 Master text styles –Second level Third level –Fourth.
EngageNY.org ©2014 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
The Animated Sequence Chapter 5.1 in Sketching User Experiences: The Workbook.
©2013 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
© 2015 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unported License.
E.Bertino, L.Matino Object-Oriented Database Systems 1 Chapter 5. Evolution Seoul National University Department of Computer Engineering OOPSLA Lab.
Sketching Vocabulary Chapter 3.4 in Sketching User Experiences: The Workbook Drawing objects, people, and their activities.
© 2014 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unported License.
©2013 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Win8 on Intel Programming Course Paul Guermonprez Intel Software
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
Design of Everyday Things Part 2: Useful Designs? Lecture /slide deck produced by Saul Greenberg, University of Calgary, Canada Images from:
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
EngageNY.org ©2012 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
© 2014 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unported License.
Images of pesticides By: Leslie London, University of Cape Town This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.5.
A Survey of Object-Oriented Concept Oscar Nierstrasz.
Traits by Nathanael Sch ä rli, St é phane Ducasse, Oscar Nierstrasz and Andrew P. Black.
EngageNY.org ©2012 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
Companion slides for The Art of Multiprocessor Programming by Maurice Herlihy & Nir Shavit Concurrent Skip Lists.
© 2015 Core Knowledge Foundation. This work is licensed under a Creative Commons Attribution- NonCommercial-ShareAlike 3.0 Unported License.
1 Licence This presentation document is one used by Ryan Cartwright of Equitas IT Solutions [1] and is Copyright © 2008 Ryan P. Cartwright / Equitas IT.
Sketching Vocabulary Chapter 3.4 in Sketching User Experiences: The Workbook Drawing objects, people, and their activities.
Methodology Overview 2 basics in user studies Lecture /slide deck produced by Saul Greenberg, University of Calgary, Canada Notice: some material in this.
Types of Programming Languages
FOTW Worksheet Slides Christopher Penn, Financial Aid Podcast Student Loan Network.
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  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  Cassou, et al. Traits at Work: the design of a new trait-based stream library. JCLSS 2009 © Oscar Nierstrasz Traits 2

© Oscar Nierstrasz ST — Smalltalk Basics 2.3 Roadmap  Why traits?  Traits in a Nutshell  Case study — Streams  Traits in Pharo  Future of Traits

© Oscar Nierstrasz ST — Smalltalk Basics 2.4 Roadmap  Why traits?  Traits in a Nutshell  Case study — Streams  Traits in Pharo  Future of 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

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

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) Rectangle selectors select: [:s | Morph selectors includes: s] Rectangle selectors select: [:s | Morph selectors includes: s]

Mixins extend single inheritance with features that can be mixed into a class © Oscar Nierstrasz Traits 8

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

© Oscar Nierstrasz ST — Smalltalk Basics 2.10 Roadmap  Why traits?  Traits in a Nutshell  Case study — Streams  Traits in Pharo  Future of 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 species and: [self origin = aRectangle origin] and: [self corner = aRectangle corner] = aRectangle ^ self species = aRectangle species and: [self origin = aRectangle origin] and: [self corner = aRectangle corner]

Class = superclass + state + traits + glue © Oscar Nierstrasz Traits 12 The class retains full control of the composition

Both traits and classes can be composed of traits © Oscar Nierstrasz Traits 13 Trait named: #NSTPuttablePositionableStream uses: NSTPuttableStream + NSTPositionableStream category: 'Nile-Base-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' Object subclass: #NSTextStream uses: NSTPuttablePositionableStream + NSTCharacterWriting instanceVariableNames: 'collection position writeLimit readLimit' classVariableNames: '' poolDictionaries: '' category: 'Nile-Clients-TextStream'

Trait composition rules 1. Class methods take precedence over trait methods 2. Conflicts are resolved explicitly 3. Traits can be flattened away © Oscar Nierstrasz Traits 14

Class methods take precedence over trait methods © Oscar Nierstrasz Traits 15 RectangleMorph>>printOn: prevails over Morph>>printOn:

Trait composition rules 1. Class methods take precedence over trait methods 2. Conflicts are resolved explicitly 3. Traits can be flattened away © Oscar Nierstrasz Traits 16

Conflicts are resolved explicitly © Oscar Nierstrasz Traits 17 RectangleMorph subclass: #Morph uses: {#rectanglePrintOn: -> #printOn:} – {#align:with:. #topRight. … } instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Morphic-TraitsDemo' RectangleMorph subclass: #Morph uses: {#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 1. Class methods take precedence over trait methods 2. Conflicts are resolved explicitly 3. Traits can be flattened away © Oscar Nierstrasz Traits 18

Traits can be flattened away © Oscar Nierstrasz Traits 19 A class using traits is equivalent to class that defines those traits locally

© 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.

The trouble with Streams © Oscar Nierstrasz Traits 21 unused state methods too high copying canceling reimplementation

The Nile core © Oscar Nierstrasz Traits 22

Nile Stream classes © Oscar Nierstrasz Traits 23 no methods too high no copying no unused state no reimplementation limited canceling

Other Nile Stream classes © Oscar Nierstrasz Traits 24

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

© Oscar Nierstrasz ST — Smalltalk Basics 2.26 Roadmap  Why traits?  Traits in a Nutshell  Case study — Streams  Traits in Pharo  Future of Traits

© Oscar Nierstrasz ST — Understanding Classes and Metaclasses 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 Traits in Pharo 1.0 Object subclass: #Behavior uses: { #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 29 navigationbrowsingrequired methods

Traits can be manipulated from the browser © Oscar Nierstrasz Traits 30

Traits and Classes share common behaviour Traits 31

Can classes share compiled methods from traits? © Oscar Nierstrasz Traits 32 Two problems: 1.super is statically bound 2.compiled methods know their class ⇒ methods are copied to method dictionaries when they are installed

© Oscar Nierstrasz ST — Smalltalk Basics 2.33 Roadmap  Why traits?  Traits in a Nutshell  Case study — Streams  Traits in Pharo  Future of 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

© Oscar Nierstrasz Traits 1.35 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. License