Partial classes

Regan Heath regan at netmail.co.nz
Tue Jun 26 03:10:56 PDT 2012


On Tue, 26 Jun 2012 02:50:46 +0100, Kapps <opantm2+spam at gmail.com> wrote:

> Any reason that just using mixin(import("myclass.partial.d")) wouldn't  
> work?

It /could/ work if you were careful about what you were mixing in, and  
into what.  In C# partial classes look like this (bare minimum).

[File1]
partial class Foo
{
}

[File2]
partial class Foo
{
}

And then, for example, the form builder gui would automatically add  
members and methods to File1 while user created content would be added by  
the user to File2.  At compile time, the two files are compiled as one  
class.

Alternately File1 may be produced by a tool like sqlmetal once, or  
multiple times while the user added content in File2 would remain  
completely under the users control.

So, yes, in D we could have:

[File1]
partial class Foo
{
  .. compiler/form builder content ..
mixin(import("File2.d"))
}

[File2]
void foo()
{
}

But, note how File2 does not (cannot) have the class declaration, etc.   
Alternately you could go the other way..

[File1]
partial class Foo
{
   .. user content ..
   mixin(import("File2.d"))
}

[File2]
.. compiler/form builder content ..

So the compiler file has no class declaration, but then the user would  
have to be careful not to remove the mixin.

Either way, it's just not quite as "nice" as the C# feature.

Implementing the C# feature in D could be trivial with some limitations.   
i.e.

1. Partial classes must be in similarly named files i.e. foo_1.d and  
foo_2.d (where foo is user defined by _1 and _2 are mandatory).

2. Partial classes must be compiled on the same command line and produce  
foo.o (note _1 and _2 are dropped and only 1 object file is produced).

.. and so on.  I am no expert on compiling/linking so there may be  
complications to the feature, but essentially it could be as simple as  
compiling the first file, pretending you haven't seen the } ending the  
class definition, opening the next file and ignoring the "partial class"  
line and continuing as if they were 1 file.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list