Մասնակից:Sonaisrayel/Ավազարկղ
Trait ֊ը գաղափար է , որն օգտագործվում է Օբյեկտ կողմնորոշված ծրագրավորման մեջ և իրենից ներկայացնում է մեթոդների շարք, որոնք օգտագործվում են դասի ֆունկցիոնալությունն ընդլայնելու համար[1][2]։
Բնութագիր
[խմբագրել | խմբագրել կոդը]Trait֊ը տրամադրում է մեթոդների հավաքածու , որոնք իրականացնում են դասի վարքագիծը և պահանջում են , որ դասը կիրառի մի շարք մեթոդներ, որոնք s 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, 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:[2][1]
- 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.[3]
Աջակցող լեզուներ
[խմբագրել | խմբագրել կոդը]Traits come originally from the programming language Self[4] 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.[5]
- C++: Used in Standard Template Library and the C++ standard library to support generic container classes[6][7] and in the Boost TypeTraits library.[8]
- Curl: Abstract classes as mixins permit method implementations and thus constitute traits by another name.[փա՞ստ]
- D: Since version 2.003, the __traits language extension[9] and std.traits module[10] 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.[11]
- ECMAScript: Since Edition 8 (June 2017)[12]
- Fortress[13]
- Groovy: Since version 2.3[14]
- Haxe: Since version 2.4.0[15]. Called Static Extension[16] in the manual, it uses
using
keyword - Java: Since version 8, Java has support for default methods,[17] which have some properties of traits.[18][19][20]
- JavaScript: Traits can be implemented via functions and delegations[21] or through libraries that provide traits.[22][23][24]
- Julia: Several packages implement traits, e.g., [25]
- Kotlin: Traits have been called interfaces[26] since M12.[27]
- Lasso[28]
- OCaml: Traits can be implemented using a variety of language features: module and module type inclusion, functors and functor types, class and class type inheritance, et cetera.
- 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.[29]
- PHP: Since version 5.4,[30][31] 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,[32][33] or via higher-order mixin classes[34]
- Racket: Supports traits as a library and uses macros, structures, and first-class classes to implement them.[35]
- Ruby: Module mixins can be used to implement traits.[36]
- Rust[37]
- Scala[38][39] trait is builtin supported with the key word
trait
. - Smalltalk: Traits are implemented in two dialects of Smalltalk, Squeak[3] and Pharo.[40]
- Swift: Traits can be implemented with protocol extensions.[41]
Օրինակներ
[խմբագրել | խմբագրել կոդը]PHP
[խմբագրել | խմբագրել կոդը]This example uses a trait to enhance other classes:
// The template
trait TSingleton
{
private static $_instance = null;
private function __construct(){} // Must have private default constructor and be aware not to open it in the class
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)
{
// …
}
}
Rust
[խմբագրել | խմբագրել կոդը]A trait in Rust declares a set of methods that a type must implement[42]. Rust compilers require traits to be explicated, which ensures the safety of generics in Rust. It also helps to avoid major problems of templates in C++.
// type T must have the "Ord" trait
// so that ">" and "<" operations can be done
fn get_max<T: Ord>(a: &[T]) -> Option<&T> {
let mut result = a.get(0)?;
for n in a {
if *n > *result {
result = &n;
}
}
Some(result)
}
To simplify tedious and repeated implementation of traits like Debug
and Ord
, derive
can be used to request compilers to generate certain codes automatically[43]. Derivable traits include: Clone
, Copy
, Debug
, Default
, PartialEq
, Eq
, PartialOrd
, Ord
and Hash
.
Տես նաև
[խմբագրել | խմբագրել կոդը]- Ընդլայնման մեթոդները
- Ինտերֆեյս (Օբյեկտ կողմորոշված ծրագրավորում)
- UFCS
Հղումներ
[խմբագրել | խմբագրել կոդը]- ↑ 1,0 1,1 Fisher, Kathleen; Reppy, John (2004). A typed calculus of traits (PDF). 11th Workshop on Foundations of Object-oriented Programming. University of Chicago.
- ↑ 2,0 2,1 Fisher, Kathleen; Reppy, John (2003). «Statically typed traits» (PDF). University of Chicago. Արխիվացված է օրիգինալից (PDF) May 17, 2004-ին.
{{cite journal}}
: Cite journal requires|journal=
(օգնություն); Unknown parameter|deadurl=
ignored (|url-status=
suggested) (օգնություն) - ↑ 3,0 3,1 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. 2743: 248–274.
- ↑ 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. էջեր 1–9. doi:10.1145/966873.806468.
- ↑ 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. Vol. 5653. Springer-Verlag. էջեր 220–243. doi:10.1007/978-3-642-03013-0_11. ISBN 978-3-642-03012-3.
- ↑ «iterator_traits<Iterator>». Standard Template Library. SGI.
- ↑ Myers, Nathan C. (June 1995). «Traits: a new and useful template technique». C++ Report. Վերցված է January 23, 2016-ին.
- ↑ Abrahams, David. «Generic Programming Techniques: Traits». Boost C++ Libraries. Վերցված է January 23, 2016-ին.
- ↑ «Traits». The D Language Reference. Digital Mars. Վերցված է January 23, 2016-ին.
- ↑ «std.traits». The D Language — Library Reference. Digital Mars. Վերցված է January 23, 2016-ին.
- ↑ «Classes». The D Language Reference. Digital Mars. Վերցված է January 23, 2016-ին.
- ↑ «ECMAScript 2017 Language Specification (ECMA-262, 8th edition)». Ecma International. June 2017.
- ↑ Steele, Guy; Maessen, Jan-Willem (June 11, 2006). «Fortress Programming Language Tutorial» (PDF). Sun Microsystems. Վերցված է January 23, 2016-ին.
- ↑ «Object Orientation: Traits». The Groovy Programming Language. Վերցված է January 23, 2016-ին.
- ↑ «Haxe 2.4.0 - Haxe - The Cross-platform Toolkit». Haxe - The Cross-platform Toolkit. Վերցված է 2017-09-12-ին.
- ↑ «Manual - Haxe - The Cross-platform Toolkit». Haxe - The Cross-platform Toolkit. Վերցված է 2017-09-12-ին.
- ↑ «Default Methods». The Java Tutorials. Oracle. Վերցված է January 23, 2016-ին.
- ↑ 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.
- ↑ «Java 8 default methods as traits: safe?». Quora. February 23, 2015. Վերցված է January 24, 2016-ին.
- ↑ Forslund, Emil (February 3, 2016). «Definition of the Trait Pattern in Java». Age of Java. Վերցված է February 3, 2016-ին.
- ↑ Seliger, Peter (April 11, 2014). «The Many Talents of JavaScript». Վերցված է January 23, 2015-ին.
- ↑ «Traits.js: Traits for JavaScript». Վերցված է January 23, 2016-ին.
- ↑ Van Cutsem, Tom; Miller, Mark S. (2012). «Robust Trait Composition for Javascript» (PDF). Science of Computer Programming: Special Issue on Advances in Dynamic Languages. Վերցված է January 23, 2016-ին.
- ↑ «CocktailJS». Վերցված է January 23, 2016-ին.
- ↑ mauro3. «SimpleTraits.jl». Վերցված է March 23, 2017-ին.
{{cite web}}
: CS1 սպաս․ թվային անուններ: authors list (link) - ↑ «Interfaces». Kotlin Reference. JetBrains. Վերցված է January 23, 2016-ին.
- ↑ Breslav, Andrey (May 29, 2015). «Kotlin M12 is out!». Kotlin Blog. JetBrains. Վերցված է January 23, 2016-ին.
- ↑ «Traits». Lasso Language Guide. LassoSoft. January 6, 2014. Վերցված է January 23, 2016-ին.
- ↑ chromatic (April 30, 2009). «The Why of Perl Roles». Վերցված է January 23, 2016-ին.
- ↑ «Traits». PHP Documentation. The PHP Group. Վերցված է January 23, 2016-ին.
- ↑ Marr, Stefan (January 9, 2011). «Request for Comments: Horizontal Reuse for PHP». PHP.net wiki. The PHP Group. Վերցված է January 31, 2011-ին.
- ↑ Perä, Teppo. «py3traits Documentation». Վերցված է January 23, 2016-ին.
- ↑ Perä, Teppo (2015-03-25). «py2traits». Վերցված է January 23, 2016-ին.
- ↑ «Higher Order Mixin Classes». Արխիվացված է օրիգինալից 2016-10-09-ին.
- ↑ «Traits». The Racket Reference. Վերցված է January 23, 2016-ին.
- ↑ David Naseby (February 14, 2004). «Traits in Ruby». Ruby Naseby. Վերցված է January 23, 2016-ին.
- ↑ «Traits». The Rust Programming Language. Վերցված է January 23, 2016-ին.
- ↑ «Traits». A Tour of Scala. École polytechnique fédérale de Lausanne. Վերցված է January 23, 2016-ին.
- ↑ Neward, Ted (April 29, 2008). «The busy Java developer's guide to Scala: Of traits and behaviors». IBM developerWorks. IBM. Վերցված է January 23, 2016-ին.
- ↑ «Traits in 10 minutes». Pharo: The CollaborActive Book. Վերցված է January 23, 2016-ին.
- ↑ Hollemans, Matthijs (July 22, 2015). «Mixins and Traits in Swift 2.0». Վերցված է January 23, 2016-ին.
- ↑ «Traits - Introduction to Programming Using Rust».
- ↑ «Traits - the Rust Programming Language».
Արտաքին հղումներ
[խմբագրել | խմբագրել կոդը]- «Traits: Composable Units of Behavior». Software Composition Group. University of Bern.