D mixins
Tom S
h3r3tic at remove.mat.uni.torun.pl
Thu Jan 4 13:14:43 PST 2007
janderson wrote:
> I'm trying to explain to a friend on msn what mixins are. Can you
> provide some good examples, that would be difficult otherwise?
>
> -Joel
They're a convenient tool for inserting parametrized code into scopes -
in some cases this can be partially accomplished in C++ through the
(ab)use of multiple inheritance. For instance:
template <typename SomePolicy>
struct Foo : SomePolicy {
};
could be coded in D as:
struct Foo(SomePolicy) {
mixin SomePolicy;
}
It doesn't seem like a big win in this case, but when the Policy must
use the Foo struct for some reason, the C++ approach would be to
specialize the policy on the struct's type, like
struct Foo : SomePolicy<Foo>
while in D, the mixin just knows about the struct's type because mixing
it in means that it becomes a part of the struct's declaration, so for
instance typeof(this) inside a mixin will work just like in the struct
itself.
D further benefits from the fact that mixins can be easily named - it
helps avoid naming conflicts.
--
Tomasz Stachowiak
More information about the Digitalmars-d
mailing list