23
votes
Should a trait refer to parent methods?
It's fine if a trait depends on methods in the class into which it is embedded. But these dependencies should be explicit, by declaring an abstract function. PHP can then check that such a method ...
8
votes
Should a trait refer to parent methods?
Yes, it's a code smell. If a trait is used to make code more portable, then then this sort of thing makes it less portable. You shouldn't do it. Without having a more concrete example it looks like ...
6
votes
Accepted
Signature for a Rust method that modifies object but might also drop it?
Although the Drop trait's drop() method is declared with &mut self, it is not actually possible to call that method directly. Instead you would have to use std::mem::drop() which takes ownership ...
4
votes
How to track C++ class traits?
If these traits are part of the type's interface, they should ideally be asserted close to the declaration of that type. A static_assert is the ideal way to do that, since compilation will fail (...
4
votes
Languages with PHP-like traits?
In PHP, "traits" are a way to reuse code in classes, providing a mechanism for horizontal code reuse. "Traits" allow you to define methods that can be used in multiple classes ...
3
votes
How to track C++ class traits?
Are these acceptable as unit tests?
No. These are not acceptable unit tests. Unit tests should check behaviour rather than implementation. The unit test you have is not checking any behaviour of ...
3
votes
Accepted
Interface + Trait vs Abstract Class
I think the easiest way to approach this is by splitting the process of making these design decisions into separate steps, first thinking about the "interface", then about the implementation:...
2
votes
Interface + Trait vs Abstract Class
I am going to answer generically rather giving specifics for a particular language.
Three concepts worth considering are:
A contract - which states that a particular class supports a set of
logically ...
2
votes
Accepted
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
Yes, you really should factor out adapting the argument, but dereferenceIfNeeded() is a mouthful. Why not tryDeref()?
As you want to tackle all (smart)-pointer, and references, what about std::...
2
votes
What's the difference between a Mixin and a Trait?
PHP does not have concept of mixins, however it has traits which look like mix of traits and mixins from hack/scala:
Can define properties.
Can define constructors.
Can define abstract methods.
Does ...
2
votes
Is there a way to document required properties in traits (PHP)?
Best way I think
trait ProductTrait
{
final private function getTable() {
if ( empty($this->table_product) ) {
throw new \Exception('$table_product must be defined in '. ...
1
vote
How to track C++ class traits?
I, too, have a preference for regular types and value semantics. But I don't encode preferences into unit tests. Unit tests exist to enforce the requirements. Very occasionally, the best way to do ...
1
vote
Comparision of modeling with inheritance vs idiomatic trait based composition
You can create a trait for Observable methods, implement the trait for your particular InterestingThing.
If you want Dude to be observable, you must also implement the trait for Dude. One thing that ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
trait × 20php × 8
scala × 6
design-patterns × 3
rust × 3
c++ × 2
programming-languages × 2
inheritance × 2
multiple-inheritance × 2
mixins × 2
delegation × 2
design × 1
object-oriented × 1
unit-testing × 1
object-oriented-design × 1
.net × 1
interfaces × 1
naming × 1
class-design × 1
functions × 1
abstraction × 1
strings × 1
mocking × 1
code-smell × 1
generics × 1