Traits Composable Units of Behavior
Traits are a simple composition mechanism for structuring object-oriented programs. A Trait is essentially a parameterized set of methods; it serves as a behavioral building block for classes and is the primitive unit of code reuse. With Traits, classes are still organized in a single inheritance hierarchy, but they can make use of Traits to specify the incremental difference in behavior with respect to their superclasses.
Unlike mixins and multiple inheritance, Traits do not employ inheritance as the composition operator. Instead, Trait composition is based on a set of composition operators that are complementary to single inheritance and result in better composition properties.
We have implemented Traits in Squeak, an open-source Smalltalk-80 dialect. In the meantime, Traits have had a significant practical impact and have been ported to various other languages.
Papers
The following papers give an overview of Traits and show their practical usability. Note that the material of all these papers and more is also available in Nathanael Schärli's dissertation Traits Composing Classes from Behavioral Building Blocks.
- Traits Composable Units of Behavior is the concept paper introducing Traits. It also compares Traits to mixins and multiple inheritance, summarizes a formal model for Traits, briefly discusses our implementation and gives an overview of a realistic application: refactoring the Smalltalk collection hierarchy.
- Traits: A Mechanism for Fine-grained Reuse is a new paper accepted to TOPLAS. This paper is a mature description of traits and contains a formal model of traits.
- Applying Traits to the Smalltalk Collection Hierarchy is an experience report of applying Traits to refactor the Smalltalk collection hierarchy.
- Uniform and Safe Metaclass Composition presents how traits can be used at the metalevel and offer safe metaclass composition model for Traits and defines some basic properties of Traits and classes.
- Traits: The Formal Model presents a formal model for Traits and defines some basic properties of Traits and classes.
- Traits: Tools and Methodology describes the Trait Browser and the programming methology that we have developed around the use of Traits and the Trait Browser.
- A Browser for Incremental Programming describes the virtual categories used in the Trait Browser in more details.
Implementations
Squeak Smalltalk
Our prototype version of Traits in Squeak is available for download. In addition, we are working on a clean and stable version of Traits in Squeak. The latest version is available from SqueakMap.
Perl
Stevan Little implementated Traits in Perl 5. In his series about the unfolding of the Perl 6 design, Larry Wall, the creator of Perl, gives a description of Roles, the variation of Traits that is planned as a fundamental language element of Perl 6.
Scala
Scala is a modern multi-paradigm programming language that smoothly integrates features of object-oriented and functional languages. Scala also features Traits, which is particularly interesting because they are smoothly integrated into Scala's static type system (which is similar to the type system of Java and C#).
Rotor
We are currently working on a Microsoft funded research project aimed to bring Traits to C#/Rotor, the shared source implementation of .NET.
VisualWorks Smalltalk
Terry Raymonds from Cincom ported a variation of Traits to VisualWorks Smalltalk.