[Xotcl] Mixins in XOTcl
Adam Turoff
aturoff at bna.com
Mon May 17 15:44:41 CEST 2004
Hi,
I'm a long-time Perl programmer who recently started using
XOTcl on a new
project. Support for mixins has been a tremendous boon for
productivity
on this project.
However, the standard idioms for managing mixins (mixin and
mixinappend) both
feel a little broken. For example, with "obj mixin
list_of_classes", I need
to know the full set of mixins for an object (or a class)
each time I mutate it.
Using mixinappend helps, but appending new classes to the
end of the mixin
list seems wrong, because the newly added classes will be
found *last*.
Here is why I think these idioms don't work well: in most
other languages, an
object is created through successive refinement. In
C++/Java, we start with a
base class and derive more and more specific variants on
that definition through
subclassing. I use XSLT a lot, and in XSLT, the most
specific templates are
found after templates of lesser specificity. Surely there
are other examples;
these are just the first two that come to my mind. The
underlying idiom seems
universal, at least to me.
To remedy this, I've added this primitive into my system.
Suggestions for better
names greatfully appreciated:
Object instproc insert_mixin {mixin_list} {
my mixin [append mixin_list " " [my info mixin]]
}
With this new operation, I can now derive an object through
mixins in a very
natural manner:
Car mycar
mycar insert_mixin Volkswagen
mycar insert_mixin NewBeetle
mycar insert_mixin AutomaticTransmission
mycar insert_mixin Turbo
mycar insert_mixin WasabiGreen
....
If I were to start calling methods like "max_rpm",
"engine_size", "color",
or "transmission", they can all get intercepted at the right
point in the
mixin chain, wrapping/superceding more generic methods.
Instant separation
of concerns. ;-)
Additionally, I can dynamically refine my object:
if {$add_roof_rack} {
mycar insert_mixin RoofRack
}
if {$has_sunroof} {
mycar insert_mixin SunRoof
}
Thanks for the excellent programming environment. Hope this
helps,
-- Adam
More information about the Xotcl
mailing list