Trait (computer programming)

From Wikipedia, the free encyclopedia
  (Redirected from Traits class)
Jump to: navigation, search

In computer programming, a trait is a concept used in object-oriented programming: a trait represents a collection of methods, that can be used to extend the functionality of a class. Essentially a trait is similar to a class made only of concrete methods that is used to extend another class with a mechanism similar to multiple inheritance, but paying attention to name conflicts, hence with some support from the language for a name-conflict resolution policy to use when merging.

Traits provide a "simple conceptual model for structuring object-oriented programs"[1][2] similar to mixins. Traits provide a simple way to create classes that reuse behavior from software components.

Traits are somewhat between an interface and a mixin: an interface is made only of method signatures, while a trait includes also the full method definitions, on the other side mixins include method definitions, but they can also carry state through attributes while traits usually don't.

An interface is used by a class by "implementing" its methods. A mixin is used by a class by mixing-into itself all the features of the mixin: i.e. the attributes and the methods. A trait is used by a class by merging the collection of extra methods provided with some mechanism to resolve name conflicts.

Hence an object defined as a trait is created as the composition of methods, which can be used by other classes without requiring multiple inheritance. In case of a naming collision, when more than one trait to be used by a class has a method with the same name, the programmer must explicitly disambiguate which one of those methods will be used in the class; thus manually solving the "diamond problem" of repeated inheritance. This is different from other composition methods in object-oriented programming, where conflicting names are automatically resolved by scoping rules.

Whereas mixins can be composed only using the inheritance operation, traits offer a much wider selection of operations, including[3]

  • symmetric sum: an operation that merges two disjoint traits to create a new trait
  • override (or asymmetric sum): an operation that forms a new trait by adding additional methods possibly overriding some of the methods of an existing trait
  • alias: an operation that creates a new trait by adding a new name for an existing method
  • and exclusion: an operation that forms a new trait by removing a method from an existing trait. (Combined with the alias operation yields a shallow rename operation).

A trait differs from an interface in that it provides implementations of its methods, not just type signatures.

Supported languages[edit]

Traits come from the Self programming language[5] and are supported by the following programming languages:

Traits for the Smalltalk programming language were initially developed at the Software Composition Group, University of Bern.[15] AmbientTalk combines the properties of Self traits (object-based multiple inheritance) and Smalltalk's Squeak traits (requiring explicit composition of traits by the programmer); AmbientTalk builds upon the research on stateful and freezable traits to enable state within traits, which was not allowed in the first definitions.[16]

The concept has been applied as libraries to mainstream languages like C++, PHP, and Javascript.[17]

Traits have the following properties
  • Provides a set of methods that implement behaviour.
  • Requires a set of methods that parameterize the provided behaviour.
Can be composed
  • Trait composition is symmetric[clarify] and conflicting methods are excluded from the composition.
  • Can be nested, but the nesting has no semantics for classes.

Nested traits are equivalent to flattened traits[clarify].[18] Abstract classes as mixins in the multiple-inheritance Curl programming language permit method implementations and thus constituted traits by another name. Module mixins in Ruby are similar to traits to some degree. Racket supports traits as a library and uses macros, structures, and first-class classes to implement them.


In computer programming, a traits class is a class template used to associate state and/or behaviour to a compile-time entity, typically a data type or a constant, without modifying the existing entity. In the C++ programming language and PHP programming language, this is normally achieved by defining a primary class template, and creating explicit or partial specializations for the relevant types.

It is used in Standard Template Library and the C++ standard library to support generic container classes. The technique is used extensively in the Boost TypeTraits library.

Traits function differently in PHP. Since version 5.4.0,[9] PHP allows users to specify templates that provide the ability to "inherit" from more than one (trait-)class, as a pseudo multiple inheritance. Traits in PHP are not as dynamic as C++ in using different data types.



Since version 5.4.0 PHP allows traits. This example uses one template class (trait) to enhance another class:

// The template
trait TSingleton
    private static $_instance = null;
    public static function getInstance()
        if (null === self::$_instance)
            self::$_instance = new self();
        return self::$_instance;
class FrontController
    use TSingleton;
// Can also be used in already extended classes
class WebSite extends SomeClass
    use TSingleton;

This gives the user power to simulate aspects of multiple inheritance:

trait TBounding
    public $x, $y, $width, $height;
trait TMoveable
    public function moveTo($x, $y)
        // ...
trait TResizeable
    public function resize($newWidth, $newHeight)
        // ...
class Rectangle
    use TBounding, TMoveable, TResizeable;
    public function fillColor($color)
        // ...


  1. ^ Nathanael Schärli, Stéphane Ducasse, Oscar Nierstrasz, Andrew P. Black. Traits: Composable Units of Behaviour. Proceedings of the European Conference on Object-Oriented Programming (ECOOP). Lecture Notes in Computer Science, Volume 2743, Springer-Verlag, 2003, pp. 248-274
  2. ^ Stéphane Ducasse, Oscar Nierstrasz, Nathanael Schärli, Roel Wuyts, Andrew P. Black: Traits: A mechanism for fine-grained reuse. ACM Trans. Program. Lang. Syst. 28(2): 331-388 (2006)
  3. ^ Fisher, Kathleen; Reppy, John (2003). "Statically typed traits". University of Chicago. 
  4. ^ Fisher, Kathleen; Reppy, John (2003). "A typed calculus of traits". University of Chicago. 
  5. ^ G. Curry, L. Baer, D. Lipkie, and B. Lee. Traits: An approach to multiple-inheritance subclassing. In SIGOA conference on Office Information Systems, pages 1–9, Philadelphia, Pennsylvania, USA, 1982. ACM Press
  6. ^
  7. ^
  8. ^
  9. ^ a b Marr, Stefan. "Request for Comments: Horizontal Reuse for PHP". The wiki. The PHP Group. Retrieved 31 January 2011. 
  10. ^
  11. ^
  12. ^ A Tour of Scala: Traits
  13. ^
  14. ^
  15. ^
  16. ^ Adding State and Visibility Control to Traits Using Lexical Nesting. Genoa Proceedings of the 23rd European Conference on ECOOP 2009 --- Object-Oriented Programming table of contents. Editor: Sophia Drossopoulou Department of Computing, Imperial College London, London, UK SW7 2AZ. Pages 220 - 243. Publisher Springer-Verlag Berlin, Heidelberg ©2009 ISBN 978-3-642-03012-3 doi>10.1007/978-3-642-03013-0_11. Appears in LNCS: Lecture Notes In Computer Science.
  17. ^ Robust Trait Composition for Javascript. Tom Van Cutsem, Mark S. Miller
  18. ^

External links[edit]