Traits and mixins, what are they? When should I use them? Are there similar mechanisms in other languages?

Asked

Viewed 380 times

18

I was curiously researching about here at Sopt and I realized that she is almost always associated with . But unfortunately the research did not yield me a definitive answer to the concept of trait, just how to use it.

The only question marked with trait, but without the php tag was: Typescript supports an implementation equivalent to trait? ; in response, the author of the reply spoke about mixins, which would address the case of AP.

Then I was wondering:

  • What is a trait?
  • What is a mixin?
  • What are the cases where we should use them? What are the development/maintenance problems these mechanisms proposes to solve?
  • In other languages, there are other mechanisms that solve similar problems?

1 answer

17


Each language implemented and called trait what you understood well. It is one of those things that nobody knows to define for sure.

So much trait as mixin are code reuse techniques, both make subclass, being that the first establishes a relation of subtype and the second not, although I have seen case that establishes, so I do not know if it ceased to be mixin.

The trait has no state, the mixin may have, but I’ve seen something called trait accepting state and other call mixin who does not accept.

If the mixin has been and does subtype what he is different from abstract class? Perhaps the fact that it does not allow a constructor, only the concrete class would have the constructor, the abstract class cannot be instantiated, but it can have constructors that obviously can only be called by the derived class. Maybe that’s what it is.

Some will say that putting only implementation and even having been is just an inclusion, which is a mechanism "underjudged".

What are the cases where we should use them? What are the development/maintenance problems these mechanisms proposes to solve?

It’s all about organization and reuse by solving the problems that other mechanisms have.

State inheritance, in particular, is complicated and deals with implementation detail, in addition to what is common for an object to have things to do in common with other objects, but not to be an inheritance as we normally know it. The trait solves this. What an object is able to do is different from it was derived from something.

Only implementation inheritance disorganizes the object to have a contract maintaining the implementation.

When you understand all the aspects of the mechanisms, you see that you can have an intersection of concepts, not everyone needs to be present, and when that happens you’re creating derivative mechanisms, so there are so many. It has language that preferred to have a very flexible "one" that allowed to make the derivations according to the use.

Dynamic typing languages can do mixin more easily, because it does not need the subtype to work, since the linking of the method to the object can be done at runtime, is often what the staff calls Duck Typing.

Trait is usually more interesting in static typing.

In other languages, there are other mechanisms that solve similar problems?

Yes, but not only do languages give these names to mechanisms that don’t always do this, they also do exactly or almost this with other names, or without giving a direct name to the mechanism (private inheritance, for example).

Of course interfaces and abstract classes are similar but not identical concepts. They also have the protolocos, delegations, policies, papers, concepts, type classes, just to stay in the ones I remember and know.

Completion

I’ve been studying this, but maybe because languages didn’t use them previously have little reliable information, and what you have is discrepant. But I know they are powerful mechanisms and should be used more.

I think it’s funny PHP have trait :) Interface too, class still goes.

Note that not everything is what you say it is:

  • The subject is even more extensive than I imagined. And I thought I would escape even more from the concept of delegations and protocols

  • It is true that traits and mixins are implemented in each language as they see fit, but if you are going to take a more academic side, the best article I have read and explain traits is this one: http://scg.unibe.ch/archive/papers/Scha02bTraits.pdf

  • And in this case, traits, as proposed, do not use the subclass mechanism, they always perform a Flatten of behaviors for the class where it "imports" the functions, and traits have 4 mechanisms to treat compositional scenarios between traits and collision of behaviors.

  • In this question of Quora a Quildreen Motta summarizes the article quite simply, what would be a trait: https://www.quora.com/Are-Java-8-default-methods-equivalent-to-traits

  • Note that traits in PHP for example have the aliasing mechanism for conflict resolution in the Flatten of traits.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.