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.

A mailing list is available for discussion and questions traits@iam.unibe.ch, but you need to register first.

Selected Publications

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.

Implementations

Squeak Smalltalk

Traits are available for Squeak. Starting with version 3.9, they are part of the standard distribution: Download Squeak 3.9

Perl

Stevan Little implemented Traits in Perl 5. The first Traits implementation by Stevan little is now obsolete. Stevan has created Moose, a "postmodern object system for Perl 5", that has huge impact on the Perl community. Moose has a metaobject protocol, much like CLOS or Smalltalk; the notion of traits is called "Roles".

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. See also: Updated docs on the design of Perl6 (objects and roles)

Discussions on roles vs inheritance are quite active among Perl bloggers. See for example

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#).

C#

We have worked on a Microsoft funded research project aimed to bring Traits to C#/Rotor, the shared source implementation of .NET.

Stefan Reichhart implemented a simple prototype (including examples/demos) of traits in C# during a student project. The work is accompanied by a technical report and presentation slides from the SSCLI Capstone Workshop 2005 at Microsoft Research, Redmond.

VisualWorks Smalltalk

Terry Raymonds from Cincom ported a variation of Traits to VisualWorks Smalltalk.

Last changed by admin on 21 April 2009