Questions tagged [traits]

In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs"

In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs".

Traits have the following properties

  • Provides a set of methods that implement behaviour.
  • Requires a set of methods that parameterize the provided behaviour.
  • Does not specify any state variables.
  • Never directly accesses state variables.

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.
3441 questions
29
votes
2 answers

Is Rust trait the same as Java interface

It looks to me that Rust's trait is the same thing as Java's interface - a set of functions that needs to be implemented on object. Is there a technical reason for naming it trait not interface or is it just some preference?
smokku
  • 1,256
  • 13
  • 22
28
votes
2 answers

Conflicting implementations of trait in Rust

I want to implement a custom trait for &'a str and for integer numbers up to i32, but Rust does not allow me to: use std::convert::Into; pub trait UiId { fn push(&self); } impl<'a> UiId for &'a str { fn push(&self) {} } impl>…
Slava Baginov
  • 949
  • 1
  • 8
  • 10
28
votes
1 answer

"Expected type parameter" error in the constructor of a generic struct

I am trying to store piston textures in a struct. struct TextureFactory where R: gfx::Resources { block_textures: Vec>>, } impl TextureFactory where R: gfx::Resources { fn new(window: PistonWindow) -> Self { …
Xavier Shay
  • 4,067
  • 1
  • 30
  • 54
28
votes
4 answers

Is it possible to extend a default method implementation of a trait in a struct?

In traditional object-oriented languages (e.g. Java), it is possible to "extend" the functionality of a method in an inherited class by calling the original method from the super class in the overridden version, for example: class A { public…
faiface
  • 457
  • 1
  • 5
  • 8
28
votes
5 answers

Can I simulate traits/mixins in Swift?

Does Swift have a way of mixing in traits, a la Scala? The section of the Swift book on using extensions to add protocols to existing classes comes tantalizingly close. However, since protocols can't contain an implementation, this can't be used to…
Bill
  • 44,502
  • 24
  • 122
  • 213
28
votes
6 answers

PHP trait method conflicts: trait "inheritance" and trait hierarchies

UPDATE: I am not alone in my pondering on this issue and it seems it is indeed a bug. See here. The day it is fixed is going to be a fantastic day! :) This started out as I love PHP traits! I'm going to use them everywhere! ^_^ and now it has…
user651390
28
votes
5 answers

Why can't a class extend traits with method of the same signature?

Why do I get the error below? How to workaround it? I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to…
IttayD
  • 28,271
  • 28
  • 124
  • 178
27
votes
4 answers

Are traits not simply composition?

I was reading an article about the new features in PHP 5.4.0. One of the most anticipated one being Traits. Reading up on these Traits, to see what they're all about, they simply look as compiler assisted copy-paste to me; and a language provided…
nkr1pt
  • 4,691
  • 5
  • 35
  • 55
26
votes
2 answers

Is there something wrong with an abstract value used in trait in scala?

I have trait Invoker { val method: Method } Intellij IDEA code inspection is warning me that "Abstract value used in trait". Everything compiles fine. Is there something wrong with having an abstract value in a trait? If so, how should I specify…
pondermatic
  • 6,453
  • 10
  • 48
  • 63
26
votes
1 answer

When is it appropriate to require only PartialEq and not Eq?

I am reading the Rust book and trying to understand use cases for PartialEq and Eq traits. I realise that PartialEq is for relations which are not necessarily reflexive (i.e. there can be such x that x != x) and that Eq is a marker trait which says…
dying_sphynx
  • 1,136
  • 8
  • 17
25
votes
1 answer

Is there an intrinsic reason explaining why Rust does not have higher-kinded-types?

Rust does not have higher-kinded-types. For example, functor (and thus monad) cannot be written in Rust. I would like to know if there is a deep reason explaining this and why. For instance, reason that I can understand can be that there is no…
abitbol
  • 487
  • 4
  • 8
25
votes
7 answers

how to get used traits in php-class?

Is there any function in PHP (5.4) to get used traits as array or similar: class myClass extends movingThings { use bikes, tanks; __construct() { echo 'I\'m using the two traits:' . ????; // bikes, tanks } }
Teson
  • 6,644
  • 8
  • 46
  • 69
24
votes
5 answers

Building a Singleton Trait with PHP 5.4

We recently had a discussion if it was possible to build a trait Singleton PHP Traits and we played around with it a possible Implementation but ran into issues with building one. This is an academic exercise. I know that Singletons have very little…
edorian
  • 38,542
  • 15
  • 125
  • 143
24
votes
4 answers

What kind of "Traits" are used/defined in the C++0x Standard

A trait in C++ encapsulates a family of operations that allow an Algorithm or Data Structure to operator with that type with which it is instantiated. char_traits are an example for grouping string- and file-required functions. But not all traits…
towi
  • 21,587
  • 28
  • 106
  • 187
24
votes
1 answer

Is there a trait or any convention to check if a range or a `view_facade` that owns things? (e.g. getlines)

Given auto empty_line = [](auto& str){ return str.size() == 0; }; For that fact that ranges::getlines returns an owning view_facade that owns a buffer for its frontal iterator. So we are kind of obligated to make that kind of range an lvalue before…
sandthorn
  • 2,770
  • 1
  • 15
  • 59