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
94
votes
4 answers

Why PHP Trait can't implement interfaces?

I'm wondering why PHP Trait (PHP 5.4) cannot implement interfaces. Update from user1460043's answer => ...cannot require class which uses it to implement a specific interface I understand that it could be obvious, because people could think that if…
Leto
  • 2,594
  • 3
  • 24
  • 37
93
votes
2 answers

How do I implement the Add trait for a reference to a struct?

I made a two element Vector struct and I want to overload the + operator. I made all my functions and methods take references, rather than values, and I want the + operator to work the same way. impl Add for Vector { fn add(&self, other:…
Jeremy Sorensen
  • 1,100
  • 1
  • 7
  • 14
90
votes
1 answer

How can I dispatch on traits relating two types, where the second type that co-satisfies the trait is uniquely determined by the first?

Say I have a Julia trait that relates to two types: one type is a sort of "base" type that may satisfy a sort of partial trait, and the other is an associated type that is uniquely determined by the base type. (That is, the relation from BaseType ->…
Philip
  • 7,253
  • 3
  • 23
  • 31
90
votes
4 answers

What makes something a "trait object"?

Recent Rust changes have made "trait objects" more prominent to me, but I only have a nebulous grasp of what actually makes something into a trait object. One change in particular is the upcoming change to allow trait objects to forward trait…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
87
votes
2 answers

How can I implement Rust's Copy trait?

I am trying to initialise an array of structs in Rust: enum Direction { North, East, South, West, } struct RoadPoint { direction: Direction, index: i32, } // Initialise the array, but failed. let data = [RoadPoint {…
tehnyit
  • 1,152
  • 1
  • 9
  • 16
82
votes
7 answers

What are the pros of using traits over abstract classes?

Can someone please explain traits in Scala? What are the advantages of traits over extending an abstract class?
Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69
81
votes
3 answers

How to overload class constructor within traits in PHP >= 5.4

In PHP 5, I can to overload constructors (and any others methods). But if I get some code like this: class Base { public function __construct($a, $b) { echo $a+$b; } public function sayHello() { echo 'Hello '; …
Guy Fawkes
  • 2,313
  • 2
  • 22
  • 39
79
votes
3 answers

How do I implement a trait I don't own for a type I don't own?

I wanted to implement the Shl trait for Vec, the code is below. This would make things like vec << 4 possible, which would be nice sugar for vec.push(4). use std::ops::Shl; impl Shl for Vec { type Output = Vec; fn shl(&self,…
le_me
  • 3,089
  • 3
  • 26
  • 28
75
votes
7 answers

PHP traits - defining generic constants

What is the best way to define constants that may be used by a number of classes within a namespace? I'm trying to avoid too much inheritance, so extending base classes is not an ideal solution, and I'm struggling to find a good solution using…
t j
  • 7,026
  • 12
  • 46
  • 66
74
votes
4 answers

Is it possible to use `impl Trait` as a function's return type in a trait definition?

Is it at all possible to define functions inside of traits as having impl Trait return types? I want to create a trait that can be implemented by multiple structs so that the new() functions of all of them returns an object that they can all be…
Ameo
  • 2,307
  • 5
  • 21
  • 33
73
votes
2 answers

Why is the `Sized` bound necessary in this trait?

I have a trait with two associated functions: trait WithConstructor: Sized { fn new_with_param(param: usize) -> Self; fn new() -> Self { Self::new_with_param(0) } } Why does the default implementation of the second method…
eulerdisk
  • 4,299
  • 1
  • 22
  • 21
72
votes
3 answers

How to clone a struct storing a boxed trait object?

I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box. trait Animal { fn speak(&self); } struct Dog { name: String, } impl…
Denis Kreshikhin
  • 8,856
  • 9
  • 52
  • 84
70
votes
2 answers

How do traits classes work and what do they do?

I'm reading Scott Meyers' Effective C++. He is talking about traits classes, I understood that I need them to determine the type of the object during compilation time, but I can't understand his explanation about what these classes actually do?…
rookie
  • 7,723
  • 15
  • 49
  • 59
67
votes
1 answer

What are the similarities and differences between C++'s concepts and Rust's traits?

In Rust, the main tool for abstraction are traits. In C++, there are two tools for abstractions: abstract classes and templates. To get rid of some of the disadvantages of using templates (e.g. hard to read error messages), C++ introduced concepts…
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
67
votes
2 answers

Mixing multiple traits in Scala

Quick note: Examples from the tutorial Scala for Java Refugees Part 5: Traits and Types. Suppose I have the traits Student, Worker, Underpaid, and Young. How could I declare a class (not instance), CollegeStudent, with all these traits? Note: I am…
Daniel Ribeiro
  • 3,110
  • 3
  • 23
  • 49