Bruce Eckel
Posts: 875
Nickname: beckel
Registered: Jun, 2003
|
|
Re: Mixins2
|
Posted: Oct 21, 2005 3:16 AM
|
|
Wow. I was hoping for some way to use generics here, which may simply be impossible because of the limitations of Java generics for all I can tell at this point. But if you look at the elegance and simplicity in C++ of both the creation of the classes to be mixed in, and the mixin process itself, which is:
TimeStamped<SerialNumbered<string> > mixin1, mixin2;
versus the solutions that have been proposed: A) Write lots of code (my example) B) Write at least as much code (the dynamic proxy) which is more complicated in hopes of reducing the code as things get bigger C) Create a new language (Dynaop) which also has quite a bit of complexity, I'm tempted to agree with John Lindsey and say that "Java was simply not intended to do such a thing" and attribute this to yet another failure of imagination.
The only solution that has the same elegance as the C++ approach is the MixGen compiler described in the paper "A First-Class Approach to Genericity," but that is another new language, but designed solely around the idea of creating mixins (and the necessity of creating a non-erasure-based generic system to support it).
What's interesting is that if you step back a bit, you can see that there's a group of often distinctly different mechanisms attempting to solve the same problem; in this case, the desire to implement what I'll call "functionality injection" -- to paste on new methods to existing code in some way other than actually sticking the methods in by hand in each class. Of course, we know from the example of design patterns that sometimes subtle differences in intent can produce radically different results, and yet it seems to me there is a fundamental consistency with mixins, aspects, the visitor pattern, multimethods (which are in the Nice language and will be in C# 3.0) and certainly several other techniques that don't currently come to mind. They all try to produce a more formal way to paste new methods into existing classes.
But so far, no one has jumped up waving their hand with a clever and clean way to do mixins in J2SE5; quite the contrary, the paper "A First-Class Approach to Genericity," goes to a lot of trouble to show that erasure-based generics cannot yield such a solution. It would seem that all this can only be used to motivate consideration of alternative approaches like AOP or the MixGen compiler, or possibly Nice (which does not use erasure in its generics, which suggests that it might handle mixins elegantly).
|
|