8

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 instanceof B == false and A instanceof C == false

Traits:

  • Can only provide methods, not state
  • Can declare abstract methods, that a consumer must implement
  • Cannot have constructors
  • If A implements traits B and C, A instanceof B == false and A instanceof C == false

Is this correct or am I missing anything ? Are these definitions accurate for any OO language or just for the above mentioned ones ?

  • What usage of the terms are you talking about? The original papers? Gilad Brachas PhD thesis? – Jörg W Mittag Apr 14 '18 at 21:22
  • I'm curious what properties various implementations of traits have. Same goes for mixins. The lists I gave are what I found across 2 languages, and I want to see if those lists hold true for other languages too. – bcherny Apr 14 '18 at 21:58
  • 3
    The question seems interesting and relevant, so I don't really understand the downvotes. However, the wording could be improved by avoiding the false impression of subjectivity: from your comment it turns out that you're not interested in our "favorite language" but want to see if these properties are general to mixins & traits across all languages supporting them, or if your findings only represent the Scala view of these concepts. – Christophe Apr 15 '18 at 7:06
  • 1
    @Christophe: The question as stated in the question is a subjective shopping list question, and thus off-topic for two reasons. The question as stated in the comments is a shopping list question, and thus off-topic. – Jörg W Mittag Apr 15 '18 at 7:13
  • 1
    @Christophe: Questions asking lists-of-things are off-topic. This question asks for a list of languages that have traits and mixins and a list of properties that those implementations have. That's what Google is for. It requires 0 software engineering expertise. – Jörg W Mittag Apr 15 '18 at 7:26
2

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 not support inheritance.
  • Traits are not types.

Are these definitions accurate for any OO language or just for the above mentioned ones ?

I don't think that there is even a single thing that will work in the same way in all OO languages. Even some basics like inheritance and interfaces have some variations, not mention more blurred constructs like traits/mixins.

0

Traits are static access in PHP as explained by Benjamin Eberlei on his blog. They are similar to automated copy-paste of code.

This does not mean that traits are absolutely evil, but they most of the time impose coupling issues.

Mixin in general is the usual (somewhat safe) use of multiple inheritance. Languages with multiple inheritance and by-ref (duck) typing like Python offer elegant uses for mixins.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.