Not logged in
Log in now
Create an account
Subscribe to LWN
LWN.net Weekly Edition for May 23, 2013
An "enum" for Python 3
An unexpected perf feature
LWN.net Weekly Edition for May 16, 2013
A look at the PyPy 2.0 release
PHP 5.4.0 released
Posted Mar 2, 2012 12:21 UTC (Fri) by angdraug (subscriber, #7487)
No, mixins were invented specifically to avoid multiple inheritance when reusing code from multiple classes. They only exist in languages with single inheritance. According to that Wikipedia article I linked to earlier, PHP traits are mixins, which is why I'm curious why they had to invent their own name for a well-known concept.
Posted Mar 2, 2012 15:25 UTC (Fri) by pboddie (subscriber, #50784)
Posted Mar 2, 2012 15:46 UTC (Fri) by rfunk (subscriber, #4054)
The specific example I'm familiar with is Ruby, which supports mixins ("modules"), but is otherwise an explicitly single-inheritance language. It uses a form of implicit ordered search path/chain to determine which method runs if there are multiple candidates.
Posted Mar 3, 2012 0:24 UTC (Sat) by pboddie (subscriber, #50784)
I've used mix-ins in Python where multiple inheritance is possible (according to the above definitions), but where you want to avoid method resolution uncertainty or general untidiness, although I'd argue that architectures based on mix-ins can be quite stifling and that other mechanisms exist in Python that are preferable (mostly due to Python's dynamic typing and objects-as-callables support).
Posted Mar 2, 2012 22:55 UTC (Fri) by robert_s (subscriber, #42402)
The term (& technique) is very heavily used in the Python world to perform exactly what oseemann is describing.
Posted Mar 3, 2012 0:49 UTC (Sat) by elanthis (guest, #6227)
There's plenty of reason to have them even with multiple inheritance. Mixins can implement part of one or more interfaces, can include new members and methods, and -- this is most important -- can freely call into the class using the mixin.
In C++, which has no mixins, it is common to use a template to implement a mixin. Unfortunately it is not a "real" mixin due to various limitations the inheritance-based approach imposes. You can work around most of those with a lot of overly verbose syntax using CRTP, but it'd be nice if you didn't have to.
Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds