SlideShare uma empresa Scribd logo
1 de 35
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
© 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)
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: TRectangle @ {#rectanglePrintOn: -> #printOn:}
– {#align:with: . #topRight . … }
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'Morphic-TraitsDemo'
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
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
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
© Oscar Nierstrasz
ST — Understanding Classes and Metaclasses
12.28
Traits in Pharo 1.0
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
29
navigation browsing required 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
http://creativecommons.org/licenses/by-sa/3.0/

Mais conteúdo relacionado

Destaque (20)

02 basics
02 basics02 basics
02 basics
 
5 - OOP - Smalltalk in a Nutshell (a)
5 - OOP - Smalltalk in a Nutshell (a)5 - OOP - Smalltalk in a Nutshell (a)
5 - OOP - Smalltalk in a Nutshell (a)
 
04 idioms
04 idioms04 idioms
04 idioms
 
1 - OOP
1 - OOP1 - OOP
1 - OOP
 
Uniform and Safe Metaclass Composition
Uniform and Safe Metaclass CompositionUniform and Safe Metaclass Composition
Uniform and Safe Metaclass Composition
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
2 - OOP
2 - OOP2 - OOP
2 - OOP
 
8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model8 - OOP - Smalltalk Model
8 - OOP - Smalltalk Model
 
Stoop 416-lsp
Stoop 416-lspStoop 416-lsp
Stoop 416-lsp
 
Stoop 413-abstract classes
Stoop 413-abstract classesStoop 413-abstract classes
Stoop 413-abstract classes
 
Stoop 436-strategy
Stoop 436-strategyStoop 436-strategy
Stoop 436-strategy
 
10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)10 - OOP - Inheritance (a)
10 - OOP - Inheritance (a)
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop 439-decorator
Stoop 439-decoratorStoop 439-decorator
Stoop 439-decorator
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvwStoop 301-internal objectstructureinvw
Stoop 301-internal objectstructureinvw
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 

Semelhante a 13 traits

Scam2011 syer
Scam2011 syerScam2011 syer
Scam2011 syerSAIL_QU
 
MSCX2023_Sergio Gomez_PartI
MSCX2023_Sergio Gomez_PartIMSCX2023_Sergio Gomez_PartI
MSCX2023_Sergio Gomez_PartImscx
 
Object - Oriented Programming Polymorphism
Object - Oriented Programming PolymorphismObject - Oriented Programming Polymorphism
Object - Oriented Programming PolymorphismAndy Juan Sarango Veliz
 
Threads and Concurrency Identifying Performance Deviations in Thread Pools
Threads and Concurrency Identifying Performance Deviations in Thread PoolsThreads and Concurrency Identifying Performance Deviations in Thread Pools
Threads and Concurrency Identifying Performance Deviations in Thread PoolsPushpalanka Jayawardhana
 
Object-Oriented Programming.pptx
Object-Oriented Programming.pptxObject-Oriented Programming.pptx
Object-Oriented Programming.pptxssusereae59d
 
Rapid pruning of search space through hierarchical matching
Rapid pruning of search space through hierarchical matchingRapid pruning of search space through hierarchical matching
Rapid pruning of search space through hierarchical matchinglucenerevolution
 
TeraGrid and Physics Research
TeraGrid and Physics ResearchTeraGrid and Physics Research
TeraGrid and Physics Researchshandra_psc
 
Traits: A New Language Feature for PHP?
Traits: A New Language Feature for PHP?Traits: A New Language Feature for PHP?
Traits: A New Language Feature for PHP?Stefan Marr
 

Semelhante a 13 traits (8)

Scam2011 syer
Scam2011 syerScam2011 syer
Scam2011 syer
 
MSCX2023_Sergio Gomez_PartI
MSCX2023_Sergio Gomez_PartIMSCX2023_Sergio Gomez_PartI
MSCX2023_Sergio Gomez_PartI
 
Object - Oriented Programming Polymorphism
Object - Oriented Programming PolymorphismObject - Oriented Programming Polymorphism
Object - Oriented Programming Polymorphism
 
Threads and Concurrency Identifying Performance Deviations in Thread Pools
Threads and Concurrency Identifying Performance Deviations in Thread PoolsThreads and Concurrency Identifying Performance Deviations in Thread Pools
Threads and Concurrency Identifying Performance Deviations in Thread Pools
 
Object-Oriented Programming.pptx
Object-Oriented Programming.pptxObject-Oriented Programming.pptx
Object-Oriented Programming.pptx
 
Rapid pruning of search space through hierarchical matching
Rapid pruning of search space through hierarchical matchingRapid pruning of search space through hierarchical matching
Rapid pruning of search space through hierarchical matching
 
TeraGrid and Physics Research
TeraGrid and Physics ResearchTeraGrid and Physics Research
TeraGrid and Physics Research
 
Traits: A New Language Feature for PHP?
Traits: A New Language Feature for PHP?Traits: A New Language Feature for PHP?
Traits: A New Language Feature for PHP?
 

Mais de The World of Smalltalk (17)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 

Último

Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Último (20)

Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

13 traits

  • 2. 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
  • 3. © Oscar Nierstrasz ST — Smalltalk Basics 2.3 Roadmap > Why traits? > Traits in a Nutshell > Case study — Streams > Traits in Pharo > Future of Traits
  • 4. © Oscar Nierstrasz ST — Smalltalk Basics 2.4 Roadmap > Why traits? > Traits in a Nutshell > Case study — Streams > Traits in Pharo > Future of Traits
  • 5. 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
  • 6. 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
  • 7. 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]
  • 8. Mixins extend single inheritance with features that can be mixed into a class © Oscar Nierstrasz Traits 8
  • 9. 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
  • 10. © Oscar Nierstrasz ST — Smalltalk Basics 2.10 Roadmap > Why traits? > Traits in a Nutshell > Case study — Streams > Traits in Pharo > Future of Traits
  • 11. 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]
  • 12. Class = superclass + state + traits + glue © Oscar Nierstrasz Traits 12 The class retains full control of the composition
  • 13. 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'
  • 14. 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
  • 15. Class methods take precedence over trait methods © Oscar Nierstrasz Traits 15 RectangleMorph>>printOn: prevails over Morph>>printOn:
  • 16. 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
  • 17. Conflicts are resolved explicitly © Oscar Nierstrasz Traits 17 RectangleMorph subclass: #Morph uses: TRectangle @ {#rectanglePrintOn: -> #printOn:} – {#align:with: . #topRight . … } instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Morphic-TraitsDemo' 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
  • 18. 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
  • 19. Traits can be flattened away © Oscar Nierstrasz Traits 19 A class using traits is equivalent to class that defines those traits locally
  • 20. © 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.
  • 21. The trouble with Streams © Oscar Nierstrasz Traits 21 unused state methods too high copying canceling reimplementation
  • 22. The Nile core © Oscar Nierstrasz Traits 22
  • 23. Nile Stream classes © Oscar Nierstrasz Traits 23 no methods too high no copying no unused state no reimplementation limited canceling
  • 24. Other Nile Stream classes © Oscar Nierstrasz Traits 24
  • 25. 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
  • 26. © Oscar Nierstrasz ST — Smalltalk Basics 2.26 Roadmap > Why traits? > Traits in a Nutshell > Case study — Streams > Traits in Pharo > Future of Traits
  • 27. © 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
  • 28. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 12.28 Traits in Pharo 1.0 Object subclass: #Behavior uses: TPureBehavior @ { #basicAddTraitSelector:withMethod: -> #addTraitSelector:withMethod: } instanceVariableNames: 'superclass methodDict format traitComposition localSelectors' classVariableNames: 'ObsoleteSubclasses' poolDictionaries: '' category: 'Kernel-Classes'
  • 29. OmniBrowser supports trait browsing and navigation © Oscar Nierstrasz Traits 29 navigation browsing required methods
  • 30. Traits can be manipulated from the browser © Oscar Nierstrasz Traits 30
  • 31. Traits and Classes share common behaviour Traits 31
  • 32. 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
  • 33. © Oscar Nierstrasz ST — Smalltalk Basics 2.33 Roadmap > Why traits? > Traits in a Nutshell > Case study — Streams > Traits in Pharo > Future of Traits
  • 34. 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
  • 35. © 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 http://creativecommons.org/licenses/by-sa/3.0/

Notas do Editor

  1. Warning — toy example! Some real case study examples appear later.
  2. Better would be: = aRectangle ^ self species = aRectangle species and: [self origin = aRectangle origin] and: [self corner = aRectangle corner]
  3. 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
  4. See NSCollectionStream for a real example