◦ ◦◦◦ TECHNISCHE UNIVERSITAT¨ MUNCHEN¨ ◦◦◦◦ ◦ ◦ ◦◦◦ ◦◦◦◦ ¨ ¨ ◦ ◦◦ FAKULTAT FUR INFORMATIK Programming Languages Traits Dr. Axel Simon and Dr. Michael Petter Winter term 2012 Traits 1 / 30 “Is Multiple Inheritance the holy grail of reusability?“ Learning outcomes 1 Identify problems of composition and decomposition 2 Understand semantics of traits 3 Separate function provision, object generation and class relations 4 Traits and existing program languages Traits Introduction 2 / 30 Reusability ≡ Inheritance? Codesharing in Object Oriented Systems is usually inheritance-centric. Inheritance itself comes in different flavours: I single inheritance I multiple inheritance I mixin inheritance All flavours of inheritance tackle problems of decomposition and composition Traits Problems with Inheritance and Composability 3 / 30 Streams FileStream SynchRW SocketStream read() acquireLock() read() write() releaseLock() write() read() write() SynchedFileStream SynchedSocketStream ! Duplicated Wrappers Multiple Inheritance is not applicable as super-References are statically bound ( Alternative: Mixins) Traits Problems with Inheritance and Composability Decomposition Problems 4 / 30 Streams modified FileStream SynchRW SocketStream read() acquireLock() read() write() releaseLock() write() SynchedFileStream SynchedSocketStream read() read() write() write() ! Duplicated Features read/write Code is essentially identical but duplicated Traits Problems with Inheritance and Composability Decomposition Problems 5 / 30 Oh my god, streams! SynchRW acquireLock() releaseLock() FileStream SocketStream read() read() write() write() SynchedFileStream SynchedSocketStream ! Inappropriate Hierarchies Implement methods (acquireLock/releaseLock) to high Traits Problems with Inheritance and Composability Decomposition Problems 6 / 30 Decomposition problems All the problems of duplicated Wrappers duplicated Features inappropriate Hierarchies are centered around the question “How do I distribute functionality over a hierarchy” functional decomposition Traits Problems with Inheritance and Composability Decomposition Problems 7 / 30 Are Mixins the solution? Rectangle toString() <mixin>Color toString() Rectangle+Color toString() <mixin>Border toString() Rectangle+Color +Border toString() MyRectangle toString() ! Fragile Hierarchies Linearization overrides identically named methods earlier in the chain super is not enough to sufficiently qualify inherited features, while explicit qualification makes refactoring difficult Traits Problems with Inheritance and Composability Composition Problems 8 / 30 Are Mixins the solution? Rectangle toString() <mixin>Glue1 rectToString() Rectangle+Glue1 rectToString() <mixin>Color toString() Rectangle+Glue1 +Color <mixin>Glue2 toString() colorToString() Rectangle+Glue1 +Color+Glue2 <mixin>Border toString() toString() Rectangle+Glue1+ Color+Glue2+Border toString() MyRectangle toString() ! Lack of Control and Dispersal of Glue Code Overriding methods always happens in parallel lack of control Glue code penetrates the whole hierarchy dispersal of glue code Traits Problems with Inheritance and Composability Composition Problems 9 / 30 And Multiple Inheritance? SpyCamera MountablePlane PrecisionGun shoot() fuel shoot() equipment equipment equipment CameraPlane CombatPlane download():pics reload(Ammunition) PoliceDrone ! Conflicting Features Common base classes are shared or duplicated at class level No fine-grained specification of duplication or sharing Traits Problems with Inheritance and Composability Composition Problems 10 / 30 The idea behind Traits A lot of the problems originate from the coupling of implementation and modelling Interfaces seem to be hierarchical Functionality seems to be modular ! Central idea Separate Object creation from modelling hierarchies and assembling functionality. Use interfaces to design hierarchical signature propagation Use traits as modules for assembling functionality Use classes as frames for entities, which can create objects Traits A formal model for traits 11 / 30 Classes and methods We will construct our model from the primitive sets of a countable set of method names N a countable set of method bodies B a countable set of attribute names A Values of method bodies B are extended to a flat lattice B?, with elements concrete implementations ? undefined > in conflict and the partial order ? @ m @ > for each m 2 B Definition (Method) Partial function, mapping a name to a body Definition (Method Dictionary d 2 D) Total function d : N 7! B?, and d−1(>) = ; Definition (Class c 2 C) Either nil or hα; di · c0 with α 2 A; d 2 D; c0 2 C Traits A formal model for traits 12 / 30 Traits A trait t 2 T is a function t : N 7! B? has conflicts : T 7! 2N with conflicts(t) = fl j t(l) = >g provides : T 7! 2N with provides(t) = t−1(B) selfSends : B 7! 2N , the set of method names used in self-sends N S requires : T 7! 2 with requires(t) = b2t(N ) selfSends(b) n provides(t) ... and differs from Mixins Traits are applied to a class in parallel, Mixins incrementally Trait composition is unordered, avoiding linearization problems Traits do not contain attributes, avoiding state conflicts With traits, glue code is concentrated in particular classes Trait composition principles Flat ordering All traits have the same precedence explicit disambiguation Precedence Class methods take precedence over trait methods Flattening Non-overridden trait methods have the same semantics as class methods Traits A formal model for traits 13 / 30 Trait composition Composing Classes from Traits: hα; d . ti · c0 with hα; di · c0 a class; t a composition clause with the overriding operator .: ( t(l) d(l) = ? (d . t)(l) = d(l) otherwise Composition clauses are based on trait sum: (t1 + t2)(l) = t1(l) t t2(l) ( ? if a = l exclusion: (t − a)(l) = t(l) otherwise 8 t(l) if l 6= a <> aliasing: t[a ! b](l) = t(b) if l = a ^ t(a) = ? :>> otherwise Traits A formal model for traits 14 / 30 Trait handling ! Conflicts Conflicts arise if composed traits posses methods with identical signatures Conflict traitment X Methods can be aliased (!) X Methods can be excluded X Class Methods override trait methods and sort out conflicts (.) Traits A formal model for traits 15 / 30 Decomposition XDuplicated Features ... can easily be factored out into unique traits. XInappropriate Hierarchies Trait composition as means for reusable code frees inheritance to model hierarchical relations. XDuplicated Wrappers Generic Wrappers can be directly modeled as traits. Traits Traits against the identified problems Decomposition 16 / 30 Composition XConflicting Features Traits cannot have conflicting states, and offer conflict resolving measures like exclusion, aliasing or overriding. XLack of Control and Dispersal of Glue Code The composition entity can individually choose for each feature, which trait has precedence or how composition is done. Glue code can be kept completely within the composed entity. XFragile Hierarchies Conflicts can be resolved in the glue code. Navigational glue code is avoided. Traits Traits against the identified problems Decomposition 17 / 30 Simulating Traits in C++ template<class Super> class SyncRW: virtual public Super { public: virtual int read(){ acquireLock(); int result= Super::read(); releaseLock(); return result; }; virtual void write(int n){ acquireLock(); Super::write(n); relaseLock(); }; // ... acquireLock() & releaseLock() }; Traits Traits in practice Traits as pattern in C++ 18 / 30 Simulating Traits in C++ template<class Super> class LogOpenClose: virtual public Super { public: virtual void open(){ Super::open(); log("opened"); }; virtual void close(){ Super::close(); log("closed"); }; protected: virtual void log(char*s) { ... }; }; template<class Super> class LogAndSync: virtual public LogOpenClose<Super>, virtual public SyncRW<Super> {}; Traits Traits in practice Traits as pattern in C++ 19 / 30 Simulating Traits in C++ ! What misses for full traits? Compositional expressions are not available: Aliasing Exclusion Precedence of class methods Specifying required methods Fine-grained control over duplication Type system not flexible enough But does that matter? Traits Traits in practice Traits as pattern in C++ 20 / 30 Traits as general composition mechanism ! Central Idea Separate class generation from hierarchy specification and functional modelling 1 model hierarchical relations with interfaces 2 compose functionality with traits 3 adapt functionality to interfaces and add state via glue code in classes “Simplified multiple Inheritance without adverse effects” Traits Traits in practice Traits as pattern in C++ 21 / 30 “Believe it or not – PHP ” “So let’s do a language with real traits!” Traits Traits in practice Real Traits in PHP 22 / 30 “So let’s do a language with real traits!” “Believe it or not – PHP ” Traits Traits in practice Real Traits in PHP 22 / 30 Traits in PHP trait Rectangular { private $l=3, $w=4; public function printInfo() { echo 'rectangular $l x $w';} } trait Colored { public $color= "red"; public function printInfo() { echo 'color '. $this->color;} } class ColoredRect{ use Colored, Rectangular; public function printInfo(){ Rectangular::printInfo(); echo ' with '; Colored::printInfo(); } } $o= new ColoredRect(); $o->printInfo(); Traits Traits in practice Real Traits in PHP 23 / 30 Aliasing Traits in PHP trait Rectangular { private $l=3, $w=4; public function printInfo() { echo 'rectangular $l x $w';} } trait Colored { public $color= "red"; public function printInfo() { echo 'color '. $this->color;} } class ColoredRect{ use Colored, Rectangular
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages31 Page
-
File Size-