...
re-motion
...
mixin
...
provides
...
access
...
to
...
members
...
of
...
the
...
target
...
class
...
with
...
the
...
property
...
This
...
(note
...
the
...
upper-case
...
'T'),
...
as
...
in
...
Code Block |
---|
This.SomeTargetClassMethod ()
This.SomeTargetClassProperty ()
{code}
|
Access
...
to
...
base
...
methods
...
of
...
the
...
target
...
class
...
is
...
provided
...
by
...
the
...
property
...
Base
(note
...
the
...
upper-case
...
'B'),
...
as
...
in
...
Code Block |
---|
Base.SomeTargetClassMethod ()
Base.SomeTargetClassProperty ()
{code}
That {{This}} and {{Base}} resemble their C# |
That This
and Base
resemble their C# key-word
...
counterparts
...
this
...
and
...
base
...
is
...
no
...
coincidence,
...
of
...
course.
...
They
...
serve
...
analogous
...
purposes.
...
Neither This
nor Base
would be visible in our simple mixin classes from the previous sections, because these classes have no base-class.
This
and Base
must be inherited from the generic
Mixin
class. Two versions exists:
Mixin<TThis>
– one parameter, provides theThis
propertyMixin<TThis, TBase>
– two parameters; the first provides theThis
property, the second provides theBase
property
In the following illustration, the class QuuxMixin
extends the class Foo
. Foo
has two members named Bar ()
and Baz
. In order to access Foo
's Bar ()
and Baz
via This
, QuuxMixin
must be derived from Mixin<Foo>
. In this case, you don't have access to Base
, because the single-parameter version of Mixin<>
doesn't give you any:
Note that the Foo
class is used twice here, in two different roles:
- the
Foo
passed totypeof ()
for theExtends
attribute specifies to which class the mixin is applied - the
Foo
passed as type parameter toMixin<>
specifies which members are accessible viaThis
.
A class name as TThis
parameter is not the only choice here. The alternative here is to pass an interface for providing members:
For accessing base members overridden by the mixin, you need the Base
property, provided by the interface passed as second parameter to Mixin<TThis, TBase>
:
Note that you _don't have a choice between class name and interface name for specifying where Base
members come from. Only interface names are allowed here.
As you might have noticed, working with Mixin
is constrained in various ways:
- The
TThis
parameter must be a class name or an interface name - The
TBase
parameter can only be an interface name - There is no scheme for getting ONLY
Base
, only for getting ONLYThis
- You can pass a class name or and interface as
TThis
parameter, but only an interface asTBase
parameter.
These constraints have technical reasons and are not explained in this primer. The rest of this section will refine the concepts presented so far with a more interesting example.