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

How to unit test PHP traits

I want to know if there is a solution on how to unit-test a PHP trait. I know we can test a class which is using the trait, but I was wondering if there are better approaches. Thanks for any advice in advance :) EDIT One alternative is to use the…
Ali
  • 2,993
  • 3
  • 19
  • 42
66
votes
1 answer

Traits and abstract methods override in Scala

I have a base abstract class (trait). It has an abstract method foo(). It is extended and implemented by several derived classes. I want to create a trait that can be mixed into the derived classes so that it implements foo() and then calls the…
IttayD
  • 28,271
  • 28
  • 124
  • 178
63
votes
8 answers

How would you implement a "trait" design-pattern in C#?

I know the feature doesn't exist in C#, but PHP recently added a feature called Traits which I thought was a bit silly at first until I started thinking about it. Say I have a base class called Client. Client has a single property called Name. Now…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
62
votes
3 answers

I implemented a trait for another trait but cannot call methods from both traits

I have a trait called Sleep: pub trait Sleep { fn sleep(&self); } I could provide a different implementation of sleep for every struct, but it turns out that most people sleep in a very small number of ways. You can sleep in a bed: pub trait…
Drew
  • 8,675
  • 6
  • 43
  • 41
61
votes
1 answer

Why can a Scala trait extend a class?

I see that traits in Scala are similar to interfaces in Java (but interfaces in Java extend other interfaces, they don't extend a class). I saw an example on SO about traits usage where a trait extends a class. What is the purpose of this? Why can…
Raj
  • 4,342
  • 9
  • 40
  • 45
59
votes
2 answers

Difference between Trait and an Abstract Class in PHP

I recently came across Traits in PHP and I'm trying to understand them. During my research I stumbled upon this Stack Overflow question: Traits vs. Interfaces. The accepted answer mentions the following: An interface defines a set of methods that…
simon
  • 2,896
  • 1
  • 17
  • 22
58
votes
4 answers

Where to place traits in Laravel 5?

I am using Laravel 5 and I am confused about where to place traits files in the Laravel 5 directory structure. Should they exist in public, resources or any other directory?
58
votes
1 answer

Difference between @Delegate, @Mixin and Traits in Groovy?

Would someone explain when I would want to use Groovy Traits vs. Mixins (@Mixin) vs. Delegates (@Delegate)? Maybe some trade-offs and design concerns would help. They all seem to allow for reusing multiple "classes" of behavior. Thanks. :-) This SO…
Vahid Pazirandeh
  • 1,552
  • 3
  • 13
  • 29
50
votes
10 answers

How to check if a class uses a trait in PHP?

Notice that a trait may use other traits, so the class may not be using that trait directly. And also the class may be inherited from a parent class who is the one uses the trait. Is this a question that can be solved within several lines or I would…
bijiDango
  • 1,385
  • 3
  • 14
  • 28
50
votes
2 answers

Why would I implement methods on a trait instead of as part of the trait?

While trying to understand the Any trait better, I saw that it has an impl block for the trait itself. I don't understand the purpose of this construct, or even if it has a specific name. I made a little experiment with both a "normal" trait method…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
50
votes
3 answers

Collisions with other trait methods

How can I deal with traits with methods of same name? trait FooTrait { public function fooMethod() { return 'foo method'; } public function getRow() { return 'foo row'; } } trait TooTrait { public function tooMethod()…
Run
  • 54,938
  • 169
  • 450
  • 748
49
votes
7 answers

Linearization order in Scala

I have difficulties in understanding the linearization order in Scala when working with traits: class A { def foo() = "A" } trait B extends A { override def foo() = "B" + super.foo() } trait C extends B { override def foo() = "C" +…
woodtluk
  • 935
  • 8
  • 20
48
votes
5 answers

How to mix-in a trait to instance?

Given a trait MyTrait: trait MyTrait { def doSomething = println("boo") } it can be mixed into a class with extends or with: class MyClass extends MyTrait It can also be mixed upon instantiating a new instance: var o = new MyOtherClass with…
Ant Kutschera
  • 6,257
  • 4
  • 29
  • 40
47
votes
2 answers

Using scala constructor to set variable defined in trait

If I understand correctly, traits are the closest thing to Java interfaces and class constructors automatically set the variables. But what if I have a class that extends a trait and has a constructor which sets a variable from the trait, so…
Seba Kerckhof
  • 1,294
  • 1
  • 14
  • 23
47
votes
3 answers

The trait cannot be made into an object

I have the following code: extern crate futures; // 0.1.24 use futures::Future; use std::io; struct Context; pub trait MyTrait { fn receive(context: Context) -> Future; } pub struct MyStruct { my_trait:…
Alexander
  • 833
  • 2
  • 8
  • 17