Traits: Composable Units of Behaviour

Nathanael Schärli, Stéphane Ducasse, Oscar Nierstrasz, Andrew P. BlackEcoop 2003

TL;DR

This paper introduces "traits," a novel compositional model for structuring object-oriented programs that aims to solve the long-standing problems associated with multiple inheritance and mixins. Traits provide a fine-grained unit of code reuse, allowing developers to compose classes from reusable behavior modules without the complexities and conflicts that plague traditional inheritance mechanisms.

What Problem Does This Paper Address?

Object-oriented programming's fundamental inheritance mechanism, while powerful, has historically struggled with effectively factoring out common behaviors and composing complex systems. Single inheritance, while simple, often leads to code duplication. Multiple inheritance and mixins, designed to offer more expressive reuse, introduce significant conceptual and practical problems, including ambiguity in feature inheritance (the "diamond problem"), difficulties in accessing overridden methods, and fragile hierarchies. Despite decades of research, these approaches have not achieved widespread acceptance due to their inherent complexities. This paper addresses the need for a more robust, flexible, and understandable mechanism for code reuse and composition in object-oriented programming.

Methods

The core contribution of this paper is the introduction of traits. Traits are described as lightweight, compositional units of behavior. Key characteristics and methods include:

  • Behavioral Units: Traits are groups of pure methods that serve as building blocks for classes. They do not define state variables and do not directly access state.
  • Composition over Inheritance: Classes are composed from traits by specifying "glue code" that connects them and accesses necessary state. This composition is order-independent and symmetric.
  • Flattening Property: A crucial property is that trait composition is transparent. The semantics of a class composed of traits are equivalent to a class where all methods from the traits are directly defined. This means super calls behave as expected in single inheritance.
  • Conflict Resolution: Conflicts between identically named methods from different traits are explicitly resolved by defining a method in the class or composite trait. Aliasing is provided to make conflicting methods available under new names.
  • Backward Compatibility: Traits are designed to be backward-compatible with single inheritance, complementing rather than replacing it.

Datasets

Not discussed in the paper. The paper focuses on a conceptual model and its implementation in Squeak Smalltalk, using examples to illustrate its functionality.

Key Insights and Ideas

  • Untangling Reusability and Classes: Traits separate the role of a class as an instance generator from its role as a unit of reuse. This allows for finer-grained reuse without the constraints of a rigid inheritance hierarchy.
  • Behavioral Composition: Traits focus solely on composing behavior, not state, which significantly simplifies conflict resolution and avoids many of the problems associated with multiple inheritance.
  • Flattening is Key to Understandability: The flattening property is a critical insight. It ensures that despite complex trait compositions, the code can always be viewed and understood as if it were written using traditional single inheritance, preserving understandability.
  • Order-Independent Composition: The symmetric nature of trait composition, where order doesn't matter, simplifies the process of combining components and avoids the "total ordering" problem found in mixins.
  • Traits as a "Logical Evolution": The authors position traits not as a replacement for single inheritance, but as a natural extension that addresses its limitations while retaining its simplicity and understandability.

Key Results

The paper presents a conceptual model and its implementation in Squeak Smalltalk. The key "results" are the design and properties of traits themselves, which are argued to solve the identified problems:

  • Resolution of Inheritance Problems: Traits effectively address issues like the diamond problem, ambiguity in feature access, and fragile hierarchies that plague multiple inheritance and mixins.
  • Improved Code Reuse: Traits enable more flexible and fine-grained code reuse by allowing developers to compose behavior from modular units.
  • Enhanced Understandability: The flattening property and clear composition mechanisms make code built with traits easier to understand, even when composed of many parts.
  • Realistic Application: The paper details a refactoring of the Smalltalk-80 collection hierarchy using traits, demonstrating their practical applicability and showing a reduction in code size and method count compared to the original implementation.

Limitations and Future Work

The authors acknowledge several areas for future work:

  • Namespaces and Encapsulation: Investigating the impact of namespaces and encapsulation on the flattening property.
  • Stateful Traits: Exploring the possibility of traits specifying state variables.
  • Traits Replacing Inheritance: Extending trait composition to potentially replace inheritance entirely.
  • Instance-Level Modification: Developing ways to modify the behavior of individual instances at runtime using traits.
  • Type Systems and Interfaces: Developing type systems for traits and identifying relationships between traits and interfaces.
  • Application to Other Languages: Adapting traits to languages like Java, which present syntactic and type system challenges.
  • Refactoring Complex Hierarchies: Further exploring the application of traits to refactor other complex class hierarchies.

Summary updated

BibTeX
@inproceedings{scharli2003traits,
  author = {Nathanael Schärli and Stéphane Ducasse and Oscar Nierstrasz and Andrew P. Black},
  title = {{Traits: Composable Units of Behaviour}},
  booktitle = {Ecoop},
  year = {2003},
  doi = {10.1007/978-3-540-45070-2\_12},
}
Figure