Hijacking
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Tue Aug 7 23:14:16 PDT 2007
Walter Bright wrote:
> Bill Baxter wrote:
>> Although, I generally like the idea, I fear that requiring 'override'
>> will have side effects for mixins. The writer of the mixin has no way
>> of knowing if the methods will override something or not.
>
> I didn't think of that.
>
> But I'll put the check in as a warning, and we'll see if there are real
> problems or not.
There are probably on two likely scenarios for that:
#1 - The mixin and the base class happen to have symbol overlap.
#2 - The mixin was previously used by the base class.
In the case of #1, the resulting errors could actually be a boon, alerting the user to a
potential (serious) hazard. On the other hand, if the overlap is not an issue, or even on
purpose, the workaround is clumsy and kludgy:
class B {
int foo() {...}
}
template M {
int foo() {...}
int bar() {...}
}
class D : B {
mixin M _m;
alias _m.bar bar;
override int foo () { return _m.foo(); }
}
Chances are that a relative straightforward CheckOverrides() template or CTF could be used
to auto-generate this. There is still, however, the issue of a superfluous proxy method
being written, whether by the programmer or by CheckOverrides().
In the case of #2, there are two sub-scenarios.
In the first, the situation is an accident. Perhaps D was not previously derived from B,
but is now as a result of refactoring, and the unnecessary mixin statement was simply
missed. The compiler has caught a programmer error. Good Thing.
In the second, perhaps class B had overwritten some of the mixin's methods to behave
differently, and class D intends to re-mix the default behavior (perhaps to expose its own
changes). In this case perhaps one could wrap the mixin statement and subsequent
overwrites in an 'override {...}' block and be good to go.
template M {
int foo() {...}
int bar() {...}
}
class B {
mixin M;
int foo() {...custom version...}
}
class D : B {
override {
mixin M;
int bar() {...custom version...}
}
}
It makes for nice grouping as well, even if it is a workaround.
Of course I might have missed some other case where this could happen.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list