Thread overview
How to override function?
Oct 22, 2009
Zarathustra
Oct 22, 2009
grauzone
Oct 22, 2009
Zarathustra
October 22, 2009
I would like to know, how to override function if subclass and super class are located in the same package and different modules.

For instance when they are in the same module:
//___________________________________
module main;
class Foo{

  this(){
    proc();
  }

  void proc(){
    writefln("Foo");
  }
}

class Bar : Foo{
  override void proc(){
    writefln("Bar");
  }
}

void main(){
  new Bar;
}
//___________________________________
the result is "Bar" so it's great, but:

//___________________________________
module pack.foo;

class Foo{

  this(){
    proc();
  }

  package void proc(){ // without 'package' it works well
    writefln("Foo");
  }
}

//___________________________________
module pack.bar;

class Bar : Foo{
  package override void proc(){ // without 'package' it works well
    writefln("Bar");
  }
}
//___________________________________
module main;
void main(){
  new Bar;
}
//___________________________________
the result is "Foo" so it's unexpected to me.
October 22, 2009
Zarathustra wrote:
> I would like to know, how to override function if subclass and super class are located in the same package and different modules.
> 
> For instance when they are in the same module:
> //___________________________________
> module main;
> class Foo{
> 
>   this(){
>     proc();
>   }
>     void proc(){
>     writefln("Foo");
>   }
> }
> 
> class Bar : Foo{
>   override void proc(){
>     writefln("Bar");
>   }
> }
> 
> void main(){
>   new Bar;
> }
> //___________________________________
> the result is "Bar" so it's great, but:
> 
> //___________________________________
> module pack.foo;
> 
> class Foo{
> 
>   this(){
>     proc();
>   }
>     package void proc(){ // without 'package' it works well
>     writefln("Foo");
>   }
> }
> 
> //___________________________________
> module pack.bar;
> 
> class Bar : Foo{
>   package override void proc(){ // without 'package' it works well
>     writefln("Bar");
>   }
> }
> //___________________________________
> module main;
> void main(){
>   new Bar;
> }
> //___________________________________
> the result is "Foo" so it's unexpected to me.

It's a bug. package functions are never virtual, and the "override" attribute is just ignored. Same with private. Welcome to D.
October 22, 2009
grauzone Wrote:

> Zarathustra wrote:
> > I would like to know, how to override function if subclass and super class are located in the same package and different modules.
> > 
> > For instance when they are in the same module:
> > //___________________________________
> > module main;
> > class Foo{
> > 
> >   this(){
> >     proc();
> >   }
> > 
> >   void proc(){
> >     writefln("Foo");
> >   }
> > }
> > 
> > class Bar : Foo{
> >   override void proc(){
> >     writefln("Bar");
> >   }
> > }
> > 
> > void main(){
> >   new Bar;
> > }
> > //___________________________________
> > the result is "Bar" so it's great, but:
> > 
> > //___________________________________
> > module pack.foo;
> > 
> > class Foo{
> > 
> >   this(){
> >     proc();
> >   }
> > 
> >   package void proc(){ // without 'package' it works well
> >     writefln("Foo");
> >   }
> > }
> > 
> > //___________________________________
> > module pack.bar;
> > 
> > class Bar : Foo{
> >   package override void proc(){ // without 'package' it works well
> >     writefln("Bar");
> >   }
> > }
> > //___________________________________
> > module main;
> > void main(){
> >   new Bar;
> > }
> > //___________________________________
> > the result is "Foo" so it's unexpected to me.
> 
> It's a bug. package functions are never virtual, and the "override" attribute is just ignored. Same with private. Welcome to D.

In my opinion 'private' shouldn't be virtual if sub- and sup- classes are located in different modules. Also if I have a global private function in the first module and the global function at the same name in the second module there shouldn't be conflict.

In C++ everything which is in 'source file', but not in 'header' it is nothing less than 'private'. I D it is impossible to 'hide' global function inside module (in my opinion it is bug).

I resolved 'package' function overriding problem as follow:

class Foo{
  package void delegate() proc;
  private void _proc(){ ... }
  this(){
    proc = &_proc;
  }
  package this(void delegete() o_proc){
    proc = o_proc;
  }
}

class Bar : Foo{
  private void _proc(){ ... }
  this(){
    super(&_proc);
  }
}