Trait (abstract type)
Encyclopedia
In computer programming
, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs".
Traits are similar to mixin
s, 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 abstract type
in that it provides implementations of its methods, not just type signatures.
Traits come from the Self programming language
and are supported as a native language feature in the Fortress programming language
(where they also play the role of types), in the Scala programming language
,
the Pharo
version of Squeak Smalltalk and in Perl 6
(which calls them "roles"),
as an add-on by the Moose
module for Perl 5 and the Joose
framework for JavaScript
and will be included in PHP
5.4. 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.
Traits for the Smalltalk programming language were initially developed at the Software Composition Group, University of Bern.
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs".
Traits are similar to mixin
Mixin
In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited or just reused by a subclass, while not meant for instantiation , Mixins are synonymous functionally with abstract base classes...
s, 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 abstract type
Abstract type
In programming languages, an abstract type is a type in a nominative type system which cannot be instantiated. An abstract type may have no implementation, or an incomplete implementation...
in that it provides implementations of its methods, not just type signatures.
Traits come from the Self programming language
Self programming language
Self is an object-oriented programming language based on the concept of prototypes. Essentially an extreme dialect of Smalltalk, it was used mainly as an experimental test system for language design in the 1980s and 1990s. In 2006, Self was still being developed as part of the Klein project, which...
and are supported as a native language feature in the Fortress programming language
Fortress programming language
Fortress is a programming language designed for high-performance computing. It was created by Sun Microsystems with funding from DARPA's High Productivity Computing Systems project. One of the language designers is Guy L...
(where they also play the role of types), in the Scala programming language
Scala programming language
Scala is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming. The name Scala is a portmanteau of "scalable" and "language", signifying that it is designed to grow with the demands of its users...
,
the Pharo
Pharo
Pharo is a fork of Squeak, an implementation of the object-oriented, dynamically typed, reflective programming language Smalltalk.Appearing in 2008, Pharo focuses on removing unessential code from Squeak and serves as the reference implementation of Seaside, a web application framework for...
version of Squeak Smalltalk and in Perl 6
Perl 6
Perl 6 is a major revision to the Perl programming language. It is still in development, as a specification from which several interpreter and compiler implementations are being written. It is introducing elements of many modern and historical languages. Perl 6 is intended to have many...
(which calls them "roles"),
as an add-on by the Moose
Moose (Perl)
Moose is an extension of the Perl 5 object system. It brings modern object-oriented language features to Perl 5, making object-oriented programming more consistent and less tedious.-Features:...
module for Perl 5 and the Joose
Joose (framework)
Joose is an open-source self-hosting meta object system for JavaScript with support for classes, inheritance, mixins, traits and aspect oriented programming....
framework for JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....
and will be included in PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
5.4. Abstract classes as mixins in the multiple-inheritance Curl programming language
Curl programming language
Curl is a reflective object-oriented programming language for interactive web applications whose goal is to provide a smoother transition between formatting and programming...
permit method implementations and thus constituted traits by another name.
Module mixins in Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
are similar to traits to some degree. Racket supports traits as a library and uses macros, structures, and first-class classes to implement them.
Traits for the Smalltalk programming language were initially developed at the Software Composition Group, University of Bern.