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
37
votes
5 answers

Mixing in a trait dynamically

Having a trait trait Persisted { def id: Long } how do I implement a method that accepts an instance of any case class and returns its copy with the trait mixed in? The signature of the method looks like: def toPersisted[T](instance: T, id:…
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169
37
votes
3 answers

Extend Traits with Classes in PHP?

Why we are not allowed to extend Traits with Classes in PHP? For example: Trait T { } Class C use T {} /* or */ Class C extends T {} Is there any potential conflict for such syntax? I do not think so.
Meglio
  • 1,646
  • 2
  • 17
  • 33
36
votes
10 answers

(Re)named std::pair members

Instead of writing town->first I would like to write town->name. Inline named accessors (Renaming first and second of a map iterator and Named std::pair members) are the best solutions I have found so far. My problem with named accessors is the loss…
Ali
  • 56,466
  • 29
  • 168
  • 265
36
votes
3 answers

Can traits be used on enum types?

I read through the trait documentation and found a neat definition for using traits on structs. Is it possible to use traits on enum types? I have seen answers that say no, but they are 3 years old and don't quite do what I'm trying to do. I tried…
Vergil
  • 399
  • 1
  • 3
  • 7
34
votes
5 answers

PHP instanceof for traits

What is the proper way to check if a class uses a certain trait?
xpedobearx
  • 707
  • 3
  • 8
  • 13
33
votes
3 answers

Why does returning `Self` in trait work, but returning `Option` requires `Sized`?

This trait definition compiles fine: trait Works { fn foo() -> Self; } This, however, does lead to an error: trait Errors { fn foo() -> Option; } error[E0277]: the size for values of type `Self` cannot be known at compilation time …
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
33
votes
4 answers

Define variable b of the same type as variable a

Is it possible to declare a variable var_b of the same type as another variable, var_a? For example: template void foo(T t) { auto var_a = bar(t); //make var_b of the same type as var_a } F_1 bar(T_1 t) { } F_2 bar(T_2 t) { }
user695652
  • 4,105
  • 7
  • 40
  • 58
33
votes
2 answers

Scala client composition with Traits vs implementing an abstract class

I have read that with Scala, it is generally advised to use Traits instead of Abstract classes to extend a base class. Is the following a good design pattern and layout? Is this how Traits were intended to replace Abstract? client class (with def…
kliew
  • 3,073
  • 1
  • 14
  • 25
32
votes
3 answers

Why is "abstract override" required not "override" alone in subtrait?

I read the section of Programming in Scala where abstract override is introduced, but I'm still confused by what exactly is signified by the joining of these modifiers. The snippet of code in which these modifiers is used is pasted below: trait…
Kvass
  • 8,294
  • 12
  • 65
  • 108
31
votes
2 answers

How to have a private part of a trait?

In a crate I write, I have a bunch of internal structs public to the user and that share some code. Some of the shared code is public, some is an internal implementation. To share efficiently the code, I am using macros, but now that the project has…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
31
votes
1 answer

Is there a trait supplying `iter()`?

Is there in Rust a Trait which supplies the iter() method? I only found the trait IntoIterator, which supplies into_iter(). Just to be clear here: I do not want the Iterator trait, which supplies next(), but a trait which supplies iter(). […
Tristan Storch
  • 690
  • 8
  • 18
31
votes
3 answers

Automatically implement traits of enclosed type for Rust newtypes (tuple structs with one field)

In Rust, tuple structs with only one field can be created like the following: struct Centimeters(i32); I want to do basic arithmetic with Centimeters without extracting their "inner" values every time with pattern matching, and without implementing…
30
votes
2 answers

What does "Box" mean in rust?

What does Box mean in rust? I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ('static in this case) in type parametrization ? Also what is Fn()…
soupybionics
  • 4,200
  • 6
  • 31
  • 43
30
votes
2 answers

How to use Traits - Laravel 5.2

I'm new to Traits, but I have a lot of code that is repeating in my functions, and I want to use Traits to make the code less messy. I have made a Traits directory in my Http directory with a Trait called BrandsTrait.php. And all it does is call on…
David
  • 1,987
  • 5
  • 33
  • 65
30
votes
3 answers

What does the exclamation point mean in a trait implementation?

I found in the library reference for std::rc::Rc this trait implementation impl !Send for Rc where T: ?Sized, What does the exclamation point in front of Send mean? I consulted both The Rust Programming Language¹ book and The Rust…
nalply
  • 26,770
  • 15
  • 78
  • 101