Suvarna Garge (Editor)

Trait (computer programming)

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit

In computer programming, a trait is a concept used in object-oriented programming, which represents a set of methods that can be used to extend the functionality of a class.

Contents

Characteristics

Traits both provide a set of methods that implement behaviour to a class, and require that the class implement a set of methods that parameterize the provided behaviour.

For inter-object communication (and sharing between objects), traits are somewhat between an object-oriented protocol (interface) and a mixin. An interface may define one or more behaviors via method signatures, while a trait defines behaviors via full method definitions: i.e., it includes the body of the methods. In contrast, mixins include full method definitions and may also carry state through member variable, while traits usually don't.

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 multiple 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:

  • 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 methods to an existing trait, possibly overriding some of its methods
  • alias: an operation that creates a new trait by adding a new name for an existing method
  • exclusion: an operation that forms a new trait by removing a method from an existing trait. (Combining this with the alias operation yields a shallow rename operation).
  • Traits are composed in the following ways:

  • Trait composition is commutative; the ordering of adding traits does not matter. For example, given trait S = A + B, then trait T = B + A is the same as S.
  • Conflicting methods are excluded from the composition.
  • Nested traits are equivalent to flattened traits; the composition hierarchy does not affect the traits behaviour. For example, given trait S = A + X, where X = B + C, then trait T = A + B + C is the same as S.
  • Supported languages

    Traits come originally from the programming language Self and are supported by the following programming languages:

  • AmbientTalk: Combines the properties of Self traits (object-based multiple inheritance) and Smalltalk's Squeak traits (requiring explicit composition of traits by the programmer). It builds on the research on stateful and freezable traits to enable state within traits, which was not allowed in the first definitions.
  • C++: Used in Standard Template Library and the C++ standard library to support generic container classes and in the Boost TypeTraits library.
  • Curl: Abstract classes as mixins permit method implementations and thus constitute traits by another name.
  • D: Since version 2.003, the __traits language extension and std.traits module helper templates provide compile-time traits. Together with other language features (notably templates and mixins), they allow flexible automatic generation of methods based on interfaces and types. D also allows explicit aliasing of member methods and variables, including forwarding to multiple member classes.
  • Fortress
  • Groovy: Since version 2.3
  • Java: Since version 8, Java has support for default methods, which have some properties of traits.
  • JavaScript: Traits can be implemented via functions and delegations or through libraries that provide traits.
  • Julia: Several packages implement traits, e.g.,
  • Kotlin: Traits have been called interfaces since M12.
  • Lasso
  • Perl: Called roles, they are implemented in Perl 5 libraries such as Moose, Role::Tiny and Role::Basic. Roles are part of the language in Perl 6.
  • PHP: Since version 5.4, PHP allows users to specify templates that provide the ability to "inherit" from more than one (trait-)class, as a pseudo multiple inheritance.
  • Python: Via a third-party library, or via higher-order mixin classes
  • Racket: Supports traits as a library and uses macros, structures, and first-class classes to implement them.
  • Ruby: Module mixins can be used to implement traits.
  • Rust
  • Scala trait is builtin supported with the key word trait.
  • Smalltalk: Traits are implemented in two dialects of Smalltalk, Squeak and Pharo.
  • Swift: Traits can be implemented with protocol extensions.
  • PHP

    This example uses a trait to enhance other classes:

    This allows simulating aspects of multiple inheritance:

    References

    Trait (computer programming) Wikipedia