Skip to main content
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 ...
amon's user avatar
  • 135k
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 ...
zquintana's user avatar
  • 254
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 ...
amon's user avatar
  • 135k
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 (...
amon's user avatar
  • 135k
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 ...
RemixDevelopment's user avatar
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 ...
Bernhard's user avatar
  • 456
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:...
Doc Brown's user avatar
  • 214k
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 ...
DavidT's user avatar
  • 4,273
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::...
Deduplicator's user avatar
  • 9,131
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 ...
rob006's user avatar
  • 146
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 '. ...
LinuXpert's user avatar
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 ...
Adrian McCarthy's user avatar
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 ...
Shane's user avatar
  • 163

Only top scored, non community-wiki answers of a minimum length are eligible