Trait (computer programming)
|
|
This article may be too technical for most readers to understand. (March 2012) |
In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object-oriented programs".[1][2] Traits are similar to mixins, but whereas mixins can be composed only using the inheritance operation, traits offer a much wider selection of operations, including symmetric sum, method exclusion, and aliasing. A trait differs from an interface in that it provides implementations of its methods, not just type signatures.
Traits come from the Self programming language[3] and are supported by the following programming languages:
- Fortress programming language: Where they also play the role of types.
- JavaScript: With the Joose framework or by using just this languages functional core features - a function object that implements behavior in a stateless way is delegated via
callorapplyto objects that are in need of it. - Perl 5: With the Moose module.
- Perl 6: It calls them "roles".
- Squeak Smalltalk
- Pharo
- PHP 5.4[4]
- Rust
- Scala[5][6]
Traits for the Smalltalk programming language were initially developed at the Software Composition Group, University of Bern.[7] 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.[8]
The concept has been applied as libraries to mainstream languages like C++, PHP, and Javascript.[9]
- 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 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.[10] 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.
Contents |
Implementation [edit]
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,[11] 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.
Examples [edit]
PHP [edit]
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 === static::$_instance) { static::$_instance = new static(); } return static::$_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; function fillColor($color) { // ... } }
References [edit]
- ^ 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
- ^ 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)
- ^ 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, Pennsylvannia, USA, 1982. ACM Press
- ^ Marr, Stefan. "Request for Comments: Horizontal Reuse for PHP". The PHP.net wiki. The PHP Group. Retrieved 31 January 2011.
- ^ http://www.scala-lang.org/node/126 A Tour of Scala: Traits
- ^ The busy Java developer's guide to Scala: Of traits and behaviors
- ^ http://scg.unibe.ch/cgi-bin/scgbib.cgi?query=nathanael+traits+composable+units+ecoop
- ^ 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.
- ^ Robust Trait Composition for Javascript. Tom Van Cutsem, Mark S. Miller
- ^ http://web.cecs.pdx.edu/~black/publications/TR_CSE_02-012.pdf
- ^ Marr, Stefan. "Request for Comments: Horizontal Reuse for PHP". The PHP.net wiki. The PHP Group. Retrieved 31 January 2011.