paper

Traits: A Mechanism for Fine-grained Reuse

  • Authors:

📜 Abstract

Inheritance is well-known and accepted as a mechanism for reuse in object-oriented languages. Unfortunately, due to the coarse granularity of inheritance, it may be difficult to decompose an application into an optimal class hierarchy that maximizes software reuse. Existing schemes based on single inheritance, multiple inheritance, or mixins, all pose numerous problems for reuse. To overcome these problems we propose traits, pure units of reuse consisting only of methods. We develop a formal model of traits that establishes how traits can be composed, either to form other traits, or to form classes. We also outline an experimental validation in which we apply traits to refactor a non-trivial application into composable units.

✨ Summary

Summary

The paper introduces traits as fine-grained, reusable units of behavior for object-oriented programs. A trait is a collection of methods that is independent of a class hierarchy and may both provide and require methods. Classes remain organized through single inheritance, while traits specify reusable behavioral differences that can be composed into classes or other traits.

The authors identify limitations in conventional reuse mechanisms. Single inheritance can cause duplicated methods or force behavior into inappropriate locations in a hierarchy. Multiple inheritance introduces method and state conflicts, ambiguous inheritance paths, and difficulties with reusable wrappers. Mixins support reusable wrappers but compose linearly, making composition order significant and causing fragile hierarchies and dispersed glue code.

Traits address these issues through unordered composition and explicit conflict management. Their principal operations are trait sum, overriding, exclusion, and aliasing. Trait sum is associative and commutative, so composition order does not implicitly determine behavior. Conflicts must be resolved explicitly by the composing class or composite trait. Aliasing provides access to an implementation that would otherwise be hidden by overriding, while exclusion removes selected methods from a composition. Traits contain no state, which eliminates state conflicts between reusable components.

A central semantic result is the flattening property: a class using traits has the same behavior as an equivalent class in which the trait methods have been inlined. This separates the organization of reusable behavior from runtime class semantics and permits tools to present either a compositional view or a flattened class view. The paper also formalizes method lookup, required and provided methods, well-definedness, reachability through self- and super-sends, and equivalence between refactored class hierarchies.

The implementation in Squeak Smalltalk integrates traits into the language and programming environment. The trait browser exposes provided methods, required methods, conflicts, aliases, overrides, and glue methods. The implementation shares compiled method bodies where possible and preserves the performance characteristics of equivalent single-inheritance code, apart from ordinary accessor-method costs.

The main empirical validation refactors the Smalltalk collection hierarchy. Twenty-nine classes originally containing 635 methods were reorganized using 60 traits, reducing the implementation to 567 methods and approximately 12% less source code. The refactoring also removed inherited methods that had previously been placed too high in the hierarchy and then disabled in inappropriate subclasses. Additional applications address safe metaclass composition and bootstrapping the Smalltalk kernel with traits.

Documented influence

The paper’s influence is visible in subsequent trait research and implementations. The Software Composition Group reports that traits were implemented in Squeak, became part of the standard Squeak distribution, and were ported or adapted for systems including Perl, Scala, C#, and VisualWorks Smalltalk. (scg.unibe.ch)

Later research directly cites and extends the paper’s model. For example, Typed First-Class Traits develops a statically typed language with first-class traits and dynamic inheritance, while retaining traits as a central abstraction for modular composition. (drops.dagstuhl.de) Other subsequent work, including research on nested composition, object inheritance, and formal models of dispatch and overriding, cites the paper as part of the foundations for trait and inheritance research. (drops.dagstuhl.de)

The evidence supports a concrete impact on programming-language research and on trait implementations in object-oriented languages. The sources reviewed do not establish a specific commercial product whose design can be attributed solely to this paper.