Trait (computer programming)

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.[1][2]

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:[3][4]

Traits are composed in the following ways:

Supported languages

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

Examples

PHP

This example uses a trait to enhance other classes:

// 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 allows simulating 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)
    {
        // ...
    }
}

See also

References

  1. 1 2 3 Schärli, Nathanael; Ducasse, Stéphane; Nierstrasz, Oscar; Black, Andrew P. (2003). "Traits: Composable Units of Behaviour" (PDF). Proceedings of the European Conference on Object-Oriented Programming (ECOOP). Lecture Notes in Computer Science. Springer-Verlag. 2743: 248–274.
  2. ↑ Ducasse, Stéphane; Nierstrasz, Oscar; Schärli, Nathanael; Wuyts, Roel; Black, Andrew P. (March 2006). "Traits: A mechanism for fine-grained reuse.". ACM Transactions on Programming Languages and Systems. 28 (2): 331–388. doi:10.1145/1119479.1119483.
  3. ↑ Fisher, Kathleen; Reppy, John (2003). "Statically typed traits" (PDF). University of Chicago. Archived (PDF) from the original on May 17, 2004.
  4. ↑ Fisher, Kathleen; Reppy, John (2004). A typed calculus of traits (PDF). 11th Workshop on Foundations of Object-oriented Programming. University of Chicago.
  5. ↑ Curry, Gael; Baer, Larry; Lipkie, Daniel; Lee, Bruce (1982). Traits: An approach to multiple-inheritance subclassing. SIGOA Conference on Office Information Systems. Philadelphia, Pennsylvania, USA: ACM Press. pp. 1–9.
  6. ↑ Van Cutsem, Tom; Bergel, Alexandre; Ducasse, Stéphane; De Meuter, Wolfgang (2009). Adding State and Visibility Control to Traits Using Lexical Nesting (PDF). European Conference on Object-Oriented Programming (ECOOP 2009). Lecture Notes in Computer Science. 5653. Springer-Verlag. pp. 220–243. ISBN 978-3-642-03012-3. doi:10.1007/978-3-642-03013-0_11
  7. ↑ "iterator_traits<Iterator>". Standard Template Library. SGI.
  8. ↑ Myers, Nathan C. (June 1995). "Traits: a new and useful template technique". C++ Report. Retrieved January 23, 2016.
  9. ↑ Abrahams, David. "Generic Programming Techniques: Traits". Boost C++ Libraries. Retrieved January 23, 2016.
  10. ↑ "Traits". The D Language Reference. Digital Mars. Retrieved January 23, 2016.
  11. ↑ "std.traits". The D Language — Library Reference. Digital Mars. Retrieved January 23, 2016.
  12. ↑ "Classes". The D Language Reference. Digital Mars. Retrieved January 23, 2016.
  13. ↑ Steele, Guy; Maessen, Jan-Willem (June 11, 2006). "Fortress Programming Language Tutorial" (PDF). Sun Microsystems. Retrieved January 23, 2016.
  14. ↑ "Object Orientation: Traits". The Groovy Programming Language. Retrieved January 23, 2016.
  15. ↑ "Default Methods". The Java Tutorials. Oracle. Retrieved January 23, 2016.
  16. ↑ Bono, Viviana; Mensa, Enrico; Naddeo, Marco (September 2014). Trait-oriented Programming in Java 8. International Conference on Principles and Practices of Programming on the Java Platform: virtual machines, languages, and tools (PPPJ ’14). Kraków, Poland.
  17. ↑ "Java 8 default methods as traits: safe?". Quora. February 23, 2015. Retrieved January 24, 2016.
  18. ↑ Forslund, Emil (February 3, 2016). "Definition of the Trait Pattern in Java". Age of Java. Retrieved February 3, 2016.
  19. ↑ Seliger, Peter (April 11, 2014). "The Many Talents of JavaScript". Retrieved January 23, 2015.
  20. ↑ "Traits.js: Traits for JavaScript". Retrieved January 23, 2016.
  21. ↑ Van Cutsem, Tom; Miller, Mark S. (2012). "Robust Trait Composition for Javascript" (PDF). Science of Computer Programming: Special Issue on Advances in Dynamic Languages. Retrieved January 23, 2016.
  22. ↑ "CocktailJS". Retrieved January 23, 2016.
  23. ↑ "Interfaces". Kotlin Reference. JetBrains. Retrieved January 23, 2016.
  24. ↑ Breslav, Andrey (May 29, 2015). "Kotlin M12 is out!". Kotlin Blog. JetBrains. Retrieved January 23, 2016.
  25. ↑ "Traits". Lasso Language Guide. LassoSoft. January 6, 2014. Retrieved January 23, 2016.
  26. ↑ chromatic (April 30, 2009). "The Why of Perl Roles". Retrieved January 23, 2016.
  27. ↑ "Traits". PHP Documentation. The PHP Group. Retrieved January 23, 2016.
  28. ↑ Marr, Stefan (January 9, 2011). "Request for Comments: Horizontal Reuse for PHP". PHP.net wiki. The PHP Group. Retrieved January 31, 2011.
  29. ↑ Perä, Teppo. "py3traits Documentation". Retrieved January 23, 2016.
  30. ↑ Perä, Teppo. "py2traits". Retrieved January 23, 2016.
  31. ↑ "Higher Order Mixin Classes".
  32. ↑ "Traits". The Racket Reference. Retrieved January 23, 2016.
  33. ↑ David Naseby (February 14, 2004). "Traits in Ruby". Ruby Naseby. Retrieved January 23, 2016.
  34. ↑ "Traits". The Rust Programming Language. Retrieved January 23, 2016.
  35. ↑ "Traits". A Tour of Scala. École polytechnique fédérale de Lausanne. Retrieved January 23, 2016.
  36. ↑ Neward, Ted (April 29, 2008). "The busy Java developer's guide to Scala: Of traits and behaviors". IBM developerWorks. IBM. Retrieved January 23, 2016.
  37. ↑ "Traits in 10 minutes". Pharo: The CollaborActive Book. Retrieved January 23, 2016.
  38. ↑ Hollemans, Matthijs (July 22, 2015). "Mixins and Traits in Swift 2.0". Retrieved January 23, 2016.

External links

This article is issued from Wikipedia - version of the 11/1/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.