www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - partial class definitions

reply Jakob Praher <jp hapra.at> writes:
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
Apr 02 2007
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jakob Praher" <jp hapra.at> wrote in message 
news:euqjjs$2fve$1 digitalmars.com...
 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?
Nope.
Apr 02 2007
prev sibling parent reply janderson <askme me.com> writes:
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.
Apr 02 2007
parent reply Ary Manzana <ary esperanto.org.ar> writes:
janderson escribió:
 Jakob Praher wrote:
 Or you could use mixins:
 
 template Part1
 {
     void process()
     {
         XMLNode node = this.toXML();
         //...
     }
 };
Actually, the code above dosen't compile: "parenthesized TemplateParameterList expected following TemplateIdentifier". So you have to write: template Part1() { void process() { XMLNode node = this.toXML(); //... } }; Is there any reason the parenthesis are mandatory for no-arguments templates? I propose to remove this restriction to allow cleaner syntax for templates that dosen't recieve any parameters.
Apr 02 2007
parent janderson <askme me.com> writes:
Ary Manzana wrote:
 janderson escribió:
 Jakob Praher wrote:
 Or you could use mixins:

 template Part1
 {
     void process()
     {
         XMLNode node = this.toXML();
         //...
     }
 };
Actually, the code above dosen't compile: "parenthesized TemplateParameterList expected following TemplateIdentifier". So you have to write: template Part1() { void process() { XMLNode node = this.toXML(); //... } };
Thanks, Actually I haven't tested any of the code above. You'll of course need to use import to make the other file available.
 
 Is there any reason the parenthesis are mandatory for no-arguments 
 templates?
 
 I propose to remove this restriction to allow cleaner syntax for 
 templates that dosen't recieve any parameters.
I agree, then it would make switching from classes to templates more generic.
Apr 02 2007