Questions tagged [trait]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
1 answer
259 views

Languages with PHP-like traits?

PHP have what it calls "traits" which despite the name is not like traits in Rust, Scala or other languages. In many other languages with support for traits, a trait create a is-a relation. ...
Fred's user avatar
  • 479
1 vote
1 answer
166 views

C++20, specialize struct once per type and allow member functions to take type as reference, raw, std::unique|shared_ptr or any other smart ptr

Situation I have some existing functions which expect containers which can be iterated and which have certain value_type. The value_type can be a value or some (smart) pointer. All the value_type have ...
ridilculous's user avatar
1 vote
1 answer
208 views

Signature for a Rust method that modifies object but might also drop it?

I'w writing a program in Rust that basically pushes data through a pipeline of steps that transforms it in different ways. The data is represented by an Entry, and I am designing a Step trait for the ...
Anders's user avatar
  • 1,351
6 votes
3 answers
492 views

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 ...
Louis Langholtz's user avatar
18 votes
2 answers
16k views

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() ...
Kamafeather's user avatar
10 votes
2 answers
4k views

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 ...
bcherny's user avatar
  • 253
3 votes
2 answers
788 views

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 ...
Christian Ivicevic's user avatar
7 votes
1 answer
643 views

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,...
sadiq.ali's user avatar
  • 207
5 votes
2 answers
670 views

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. ...
Igneous01's user avatar
  • 2,343
8 votes
2 answers
7k views

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 ...
TravisCarden's user avatar
6 votes
1 answer
530 views

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 ...
vadix's user avatar
  • 71
19 votes
1 answer
3k views

Why not make a language with mixin-only inheritance? [duplicate]

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 ...
Den's user avatar
  • 4,827
7 votes
2 answers
2k views

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 ...
memo's user avatar
  • 73
1 vote
0 answers
495 views

When to use trait components, objects, package objects in Scala

When I am adding dependencies in Scala, I tend to use mixin trait components for those that are inherit to the class itself. For example, DaoGenerator extends Generator However, for trait components ...
user1555300's user avatar
6 votes
1 answer
531 views

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 ...
valenterry's user avatar
  • 2,429
11 votes
2 answers
6k views

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 ...
Privateer's user avatar
  • 299
3 votes
2 answers
672 views

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, ...
valenterry's user avatar
  • 2,429
10 votes
2 answers
3k views

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 ...
valenterry's user avatar
  • 2,429
7 votes
1 answer
811 views

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 ...
valenterry's user avatar
  • 2,429