Mixins
Posted: Sun Mar 01, 2009 1:41 am
I have added initial support for mixins to the language. "mixin" is one of those terms like "object" and "class" that varies a bit from language to language. But essentially, mixins are a limited form of multiple inheritance.
My motivation is that languages such as C# and Java (and formerly Cobra) with their single inheritance, can make coding awkward when you have concerns that cut across the class hierarchy. Historically, I came out of C++ thinking that multiple inheritance was bad, but then later I came out of Python thinking that C++ was bad. Okay, well actually I already thought that before I found Python.
Anyway, there are places in the compiler implementation and my side projects where I could make use of mixins in Cobra. Having successfully used multiple inheritance in Python, I find myself missing it. This is one the few remaining areas where Cobra was really lagging behind Python. And it puts Cobra further ahead of C# and Java.
The upshot for mixins is a reduction in code and a better organized code base.
Note that you can find blog posts and other web pages addressing the limitations of single inheritance in C# and Java. They all involve an awkward manual approach that is heavy on keyboarding and yields crufty code, or they use some kind of run-time object container that dynamically creates new classes combined out of existing ones. I wanted something more natural and clean at the language level.
Note that compared to extensions, mixins allow you to also add state (vars) and properties (pro/get/set). Extensions only support methods.
This feature adds two new keywords to the language. "mixin" for declaring a mixin, and "adds" for adding mixins to a declared type. Note that adding mixins is a compile-time feature in the same way that declaring class inheritance and interface implementation are done at compile-time in declarations.
Here is the initial test case: svn:mixin-basics.cobra:1948
Here will be the latest test cases at any time: svn:Tests/150-mixins
There are lots of restrictions on mixins at this time. They cannot inherit other types or implement interfaces, for example. Over time, as we gain experience with mixins, some of these restrictions will likely be relaxed. What we have now are the very basics:
-- declare mixins
-- support for object vars, properties and methods
-- add one or more mixins to a class
As this is a fresh feature, testing is welcome!
Finally, I have stubbed out a MixIn wiki page which has some information and will grow over time.
My motivation is that languages such as C# and Java (and formerly Cobra) with their single inheritance, can make coding awkward when you have concerns that cut across the class hierarchy. Historically, I came out of C++ thinking that multiple inheritance was bad, but then later I came out of Python thinking that C++ was bad. Okay, well actually I already thought that before I found Python.
Anyway, there are places in the compiler implementation and my side projects where I could make use of mixins in Cobra. Having successfully used multiple inheritance in Python, I find myself missing it. This is one the few remaining areas where Cobra was really lagging behind Python. And it puts Cobra further ahead of C# and Java.
The upshot for mixins is a reduction in code and a better organized code base.
Note that you can find blog posts and other web pages addressing the limitations of single inheritance in C# and Java. They all involve an awkward manual approach that is heavy on keyboarding and yields crufty code, or they use some kind of run-time object container that dynamically creates new classes combined out of existing ones. I wanted something more natural and clean at the language level.
Note that compared to extensions, mixins allow you to also add state (vars) and properties (pro/get/set). Extensions only support methods.
This feature adds two new keywords to the language. "mixin" for declaring a mixin, and "adds" for adding mixins to a declared type. Note that adding mixins is a compile-time feature in the same way that declaring class inheritance and interface implementation are done at compile-time in declarations.
Here is the initial test case: svn:mixin-basics.cobra:1948
Here will be the latest test cases at any time: svn:Tests/150-mixins
There are lots of restrictions on mixins at this time. They cannot inherit other types or implement interfaces, for example. Over time, as we gain experience with mixins, some of these restrictions will likely be relaxed. What we have now are the very basics:
-- declare mixins
-- support for object vars, properties and methods
-- add one or more mixins to a class
As this is a fresh feature, testing is welcome!
Finally, I have stubbed out a MixIn wiki page which has some information and will grow over time.