partial class definitions

janderson askme at me.com
Mon Apr 2 08:13:53 PDT 2007


Jakob Praher wrote:
> hi all,
> 
> I am new to D.
> I was wondering wheather D supports some kind of partial class 
> definition, that means one class that is implemented in several 
> translation units?
> 
> 
> Foo.d:
> class Foo
> {
>    XMLNode toXML();
> 
> 
>    void process()
>    {
>        XMLNode node = this.toXML();
>        //...
>    }
> 
> }
> 
> FooXml.d:
> 
> class Foo {
> 
>    XMLNode toXML()
>    {
>       //...
>    }
> }
> 
> thanks
> -- Jakob


You could use inheritance:

  Foo.d:
  class Foo
  {
     abstract XMLNode toXML();


     void process()
     {
         XMLNode node = this.toXML();
         //...
     }

  }

 > FooXml.d:
  class Foo2 : Foo
  {

     XMLNode toXML()
     {
        //...
     }
  }

Or you could use mixins:

template Part1
{
     void process()
     {
         XMLNode node = this.toXML();
         //...
     }
};




 > FooXml.d:
  class Foo2
  {
     mixin Part1;

     XMLNode toXML()
     {
        //...
     }
  }

Or you could use a slightly more hacky import:

Part1.d
     void process()
     {
         XMLNode node = this.toXML();
         //...
     }


 > FooXml.d:
  class Foo2
  {
     import("Part1.d");

     XMLNode toXML()
     {
        //...
     }
  }

Not quite as tiny as what you request, but close enough in my opinion 
and they allow better reuse. Of course you probably would give the 
moduals better logical and reusable names.



More information about the Digitalmars-d mailing list