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
45
votes
3 answers

Is it possible to access struct fields from within a trait?

Traits are used to group some functions to be implemented from a struct, but is it possible to access struct fields from within the trait? I could imagine declaring fields inside the trait so that the fields are abstracted as well. I haven't found…
Reignbeaux
  • 1,333
  • 3
  • 12
  • 15
44
votes
4 answers

How do I return an instance of a trait from a method?

I'm trying to create a function that returns an instance of the Shader trait. Here is my drastically simplified code: trait Shader {} struct MyShader; impl Shader for MyShader {} struct GraphicsContext; impl GraphicsContext { fn…
neon64
  • 1,486
  • 2
  • 12
  • 13
44
votes
3 answers

How can I create an is_prime function that is generic over various integer types?

I just took the dive into Rust and want to make some basic math functions that are generic. I have the following is_prime function: fn is_prime(n: i64) -> bool { if n == 2 || n == 3 { return true; } else if n % 2 == 0 || n % 3 == 0…
gaigepr
  • 905
  • 1
  • 10
  • 17
43
votes
2 answers

When and why to use AsRef instead of &T

AsRef documentation writes Used to do a cheap reference-to-reference conversion. I understand reference-to-reference part what does it mean by cheap? I hope it has nothing to do with complexity theoretic (big Oh,. etc) "cheapness". Example: struct…
storm
  • 795
  • 1
  • 5
  • 12
43
votes
6 answers

Difference between trait inheritance and self type annotation

In Scala, I've seen the constructs trait T extends S and trait T { this: S => used to achieve similar things (namely that the abstract methods in S must be defined before an instance may be created). What's the difference between them? Why…
Ben Lings
  • 28,823
  • 13
  • 72
  • 81
42
votes
2 answers

How to read (std::io::Read) from a Vec or Slice?

Vecs support std::io::Write, so code can be written that takes a File or Vec, for example. From the API reference, it looks like neither Vec nor slices support std::io::Read. Is there a convenient way to achieve this? Does it require writing a…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
42
votes
4 answers

PHP type-hinting traits

I have a trait. For the sake of creativity, let's call this trait Trait: trait Trait{ static function treat($instance){ // treat that trait instance with care } } Now, I also have a class that uses this trait, User. When…
arik
  • 28,170
  • 36
  • 100
  • 156
41
votes
5 answers

How to declare traits as taking implicit "constructor parameters"?

I'm designing a class hierarchy, which consists of a base class along with several traits. The base class provides default implementations of several methods, and the traits selectively override certain methods via abstract override, so as to acts…
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
41
votes
1 answer

How do closures infer their type based on the trait they're required to implement?

I'm writing a function that accepts different trait implementors. One of them is a closure. Some closures need an argument type annotation and some don't, depending on their bodies. Example (playground): fn main() { bar(|x| x); bar(|x: bool|…
CodeSandwich
  • 1,601
  • 13
  • 23
39
votes
3 answers

Scala case class extending Product with Serializable

I am learning scala and tried following form Scala Cookbook: trait Animal trait FurryAnimal extends Animal case class Dog(name:String) extends Animal case class Cat(name:String) extends Animal Now when I did following as : val x =…
optional
  • 3,260
  • 5
  • 26
  • 47
39
votes
1 answer

In Scala; should I use the App trait?

I've just started learning Scala and many of the tutorials that I'm following are using a combination of different representations for a main method. Aside from the familiar main method; there's also the use of traits App or Application. It appears…
Micheal Hill
  • 1,619
  • 2
  • 17
  • 38
39
votes
8 answers

Find out whether a C++ object is callable

Is it possible to write a type trait, say is_callable which tells if an object has an operator() defined? It is easy if the arguments to the call operator are known in advance, but not in the general case. I want the trait to return true if and…
Antoine
  • 13,494
  • 6
  • 40
  • 52
38
votes
1 answer

Is there any trait that specifies numeric functionality?

I'd like to use a trait to bound a generic type, like this hypothetical HasSQRT: fn some_generic_function(input: &T) where T: HasSQRT, { // ... input.sqrt() // ... }
Hossein Noroozpour
  • 1,091
  • 1
  • 13
  • 26
38
votes
4 answers

Overriding Doctrine Trait Properties

I know you can override a trait method by declaring it in your class, I was curious if was possible to over ride a trait Property the same way. Is this safe to do? Its not in the Documentation so I am hesitant to implement this. From the…
DanHabib
  • 824
  • 4
  • 15
  • 25
37
votes
1 answer

What are the typical use cases of an iterator_trait

I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows: template struct iterator_traits { typedef typename…
san
  • 4,144
  • 6
  • 32
  • 50