Thread overview
partial class definitions
Apr 02, 2007
Jakob Praher
Apr 02, 2007
janderson
Proposal: remove the mandatory parenthesis in a no-arguments template declaration [Was: partial class definitions]
Apr 02, 2007
Ary Manzana
Apr 02, 2007
janderson
April 02, 2007
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
April 02, 2007
"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.


April 02, 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.
April 02, 2007
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.
April 02, 2007
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.