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
461
votes
6 answers

How to override trait function and call it from the overridden function?

Scenario: trait A { function calc($v) { return $v+1; } } class MyClass { use A; function calc($v) { $v++; return A::calc($v); } } print (new MyClass())->calc(2); // should print 4 This code doesn't…
Shu
  • 4,869
  • 2
  • 15
  • 9
420
votes
11 answers

What is the difference between self-types and trait subclasses?

A self-type for a trait A: trait B trait A { this: B => } says that "A cannot be mixed into a concrete class that does not also extend B". On the other hand, the following: trait B trait A extends B says that "any (concrete or abstract) class…
Dave
  • 7,589
  • 12
  • 36
  • 42
405
votes
8 answers

What is the advantage of using abstract classes instead of traits?

What is the advantage of using an abstract class instead of a trait (apart from performance)? It seems like abstract classes can be replaced by traits in most cases.
Ralf
  • 14,655
  • 9
  • 48
  • 58
394
votes
13 answers

Traits vs. interfaces

I've been trying to study up on PHP lately, and I find myself getting hung up on traits. I understand the concept of horizontal code reuse and not wanting to necessarily inherit from an abstract class. What I don't understand is: What is the crucial…
datguywhowanders
  • 4,341
  • 6
  • 20
  • 14
373
votes
6 answers

What is a sealed trait?

Sealed classes are described in 'Programming in Scala', but sealed traits are not. Where can I find more information about a sealed trait? I would like to know, if a sealed trait is the same as a sealed class? Or, if not, what are the differences?…
John Threepwood
  • 15,593
  • 27
  • 93
  • 149
206
votes
3 answers

What is the difference between traits in Rust and typeclasses in Haskell?

Traits in Rust seem at least superficially similar to typeclasses in Haskell, however I've seen people write that there are some differences between them. I was wondering exactly what these differences are.
LogicChains
  • 4,332
  • 2
  • 18
  • 27
171
votes
2 answers

Mixins vs. Traits

What is the difference between Mixins and Traits? According to Wikipedia, Ruby Modules are sort of like traits. How so?
KaptajnKold
  • 10,638
  • 10
  • 41
  • 56
163
votes
5 answers

Traits in PHP – any real world examples/best practices?

Traits have been one of the biggest additions for PHP 5.4. I know the syntax and understand the idea behind traits, like horizontal code re-use for common stuff like logging, security, caching etc. However, I still don't know how I would make use of…
Max
  • 15,693
  • 14
  • 81
  • 131
147
votes
5 answers

Is it possible to figure out the parameter type and return type of a lambda?

Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how? Basically, I want lambda_traits which can be used in following ways: auto lambda = [](int i) { return long(i*10);…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
120
votes
1 answer

Java 8 default methods as traits : safe?

Is it a safe practice to use default methods as a poor's man version of traits in Java 8? Some claim it may make pandas sad if you use them just for the sake of it, because it's cool, but that's not my intention. It is also often reminded that…
youri
  • 3,685
  • 5
  • 23
  • 43
108
votes
2 answers

How to get a reference to a concrete type from a trait object?

How do I get Box or &B or &Box from the a variable in this code: trait A {} struct B; impl A for B {} fn main() { let mut a: Box = Box::new(B); let b = a as Box; } This code returns an error: error[E0605]: non-primitive…
Aleksandr
  • 1,755
  • 3
  • 13
  • 8
104
votes
4 answers

When to use val or def in Scala traits?

I was going through the effective scala slides and it mentions on slide 10 to never use val in a trait for abstract members and use def instead. The slide does not mention in detail why using abstract val in a trait is an anti-pattern. I would…
Mansur Ashraf
  • 1,337
  • 3
  • 9
  • 12
103
votes
2 answers

Is there a way to extend a trait in PHP?

I want to use functionality of an existing trait and create my own trait on top of it only to later apply it on classes. I want to extend Laravel SoftDeletes trait to make SaveWithHistory function, so it will create a copy of a record as a deleted…
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
101
votes
3 answers

How do I specify that a struct field must implement a trait?

I have a trait Foo pub trait Foo { fn do_something(&self) -> f64; } and a struct which references that trait pub struct Bar { foo: Foo, } Trying to compile I get error: reference to trait `Foo` where a type is expected; try `Box` or…
neil danson
  • 1,392
  • 2
  • 10
  • 10
95
votes
4 answers

Difference between Abstract Class and Trait

Possible Duplicate: Scala traits vs abstract classes What is the conceptual difference between abstract classes and traits?
Red Hyena
  • 2,988
  • 5
  • 25
  • 24
1
2 3
99 100