'final' function implementations in interface definition
HOSOKAWA Kenchi
hskwk at inter7.jp
Sun Jun 7 17:03:20 PDT 2009
Robert Clipsham Wrote:
> HOSOKAWA Kenchi wrote:
> > Robert Clipsham Wrote:
> >
> >> If you want this then you need abstract classes.
> >> http://www.digitalmars.com/d/1.0/attribute.html#abstract
> >>
> >> abstract class A
> >> {
> >> abstract void f(int);
> >> final void f_twice(int i) { f(i); f(i); }
> >> }
> >>
> >> class B : A { }
> >>
> >> (new B).f_twice(1);
> >
> > Unfortunately the derived class have already inherited another class in my code.
> > I need interface inheritance.
> >
>
> You could use a template mixin then:
>
> template MyInterfaceMethods()
> {
> final void f_twice( int i ) { f(i); f(i); }
> }
>
> interface I
> {
> void f( int );
> }
>
> class A : I
> {
> mixin MyInterfaceMethods;
> }
thanks for the solution but I think mixin is not better than global/final function.
It requires to write mixin for each inherited classes, which usually increase binary size with duplicative function instances.
More information about the Digitalmars-d
mailing list