Questions tagged [trait]
19 questions
19
votes
1 answer
Why not make a language with mixin-only inheritance?
It seems that in all class-based or prototypal OOP languages, mixins are either an afterthought or a secondary feature. However, to me it looks like traditional inheritance is just a specific case of using a single mixin.
I am assuming this is…

Den
- 4,827
- 2
- 32
- 48
17
votes
2 answers
Should a trait refer to parent methods?
Is it a code smell if the methods in my trait refer to parent::methods or to methods that are assumed to be in the utilising class?
A random (senseless) example
trait foo
{
public function bar()
{
return parent::bar() . 'baz';
…

Kamafeather
- 335
- 1
- 2
- 7
10
votes
2 answers
What's the difference between a Mixin and a Trait?
From what I can tell from Scala and Hack-
Mixins:
Can have state (ie. instance properties)
Can only provide concrete methods
Can have constructors, that are called in the same order that their classes were mixed in
If A mixes in B and C, A…

bcherny
- 253
- 2
- 6
10
votes
2 answers
Is there a way to document required properties in traits (PHP)?
I'm fairly new to using traits in PHP and am wondering if there is a way to ensure that a class including a trait has particular properties.
I know that you can use something like the following to ensure the using class has particular…

Privateer
- 289
- 2
- 7
10
votes
2 answers
Why can't the Scala compiler give pattern matching warning for nonsealed classes/traits?
If I use an unsealed trait or abstract class in Scala and then use pattern matching, I wonder, does the compiler not know at compile time for this particular patternmatch what possible implementations of this trait/class are available? So, if it…

valenterry
- 2,429
- 16
- 21
8
votes
2 answers
Is it possible to mock and inject traits in PHPUnit?
I need to extend a third party class I cannot modify. The class's dependencies are for the most part injected through the constructor, making them easy to mock. However the class also uses methods from a trait that call global services directly,…

TravisCarden
- 217
- 2
- 10
7
votes
1 answer
Comparision of modeling with inheritance vs idiomatic trait based composition
I recently I started learning Rust and Scala and what struck me was the lack of inheritance model that I'm used to in C++ and Java.
Although I can model simple things with structs and traits in Rust, I want to see a more idiomatic way to model…

sadiq.ali
- 207
- 1
- 2
7
votes
2 answers
Is trait composition an good practice?
So, I am now dealing with this relatively new codebase which uses, and sometimes feels like it abuses traits. Since I have been exposed to trait usage in a rather limited fashion, I was wondering whether the way they are using them makes sense. I…

memo
- 73
- 3
7
votes
1 answer
Why are Scala's Either and Option types not interfaces/traits but classes?
I wanted to create a class CompileResult, that can be treated like an Either type but has some additional useful methods. It should be a CompileSuccess or a CompileFailure (which, too, has some extended functionality). However I can't do that…

valenterry
- 2,429
- 16
- 21
6
votes
3 answers
How to track C++ class traits?
As a fan of regular types and value semantics, I'm keen on classes becoming more regular and being non-polymorphic. As a fan of non-throwing operations, I'm keen on operations being noexcept. I also appreciate confirmation of what special member…

Louis Langholtz
- 189
- 6
6
votes
1 answer
What is the most generic way to provide a variable amount of outputs from a Rust function?
I am currently writing an API for machine learning algorithms in Rust and I would like for a single genetic algorithm, artificial neural network, or Bayesian network to provide multiple outputs so that for instances in which there may be redundancy,…

vadix
- 71
- 3
6
votes
1 answer
Use StringLike instead of String in Scala?
Scala has the StringLike trait. Let's say I want to create a case class Name and internally it should save the name with some characters. Should I use case class Name(name: StringLike) or case class Name(name: String)?
The former is obviously more…

valenterry
- 2,429
- 16
- 21
5
votes
2 answers
Does such a design pattern exist? (Multi-Strategy/Multi-Traits)
Is there a design pattern that would allow a class from a hierarchy to 'subscribe to' concrete methods?
For example, say you have an abstract base class that requires the implementation of a method.
Public MustInherit Class Rule
' Common…

Igneous01
- 2,343
- 2
- 15
- 18
3
votes
2 answers
Using PHP traits to simulate multiple inheritance
For this question I want to present my current design and my idea of using a trait. I would like to know whether my understanding of traits is correct and whether my problem can be solved with another design not involving them.
My current class…

Christian Ivicevic
- 189
- 5
3
votes
2 answers
How to use scala case classes when delegation is needed
Let's assume in our application we want to model cars. We also want to model a car repository where we store some registered cars. How should that be modeled in scala?
Here comes my approach: First, I create a case class PlainCar. This is just a car…

valenterry
- 2,429
- 16
- 21