partial class

Ary Borenszweig ary at esperanto.org.ar
Sat Nov 1 07:50:50 PDT 2008


Alexander Pánek escribió:
> Ary Borenszweig wrote:
>> Mike James escribió:
>>> Not sure if this has been suggested before as an addition to D but 
>>> what about introducing a partial class as per C# - with all the 
>>> benefits it would bring...
>>
>> Julio's second example convinced me that partial classes are useful. 
>> But in D you can already do that, sort of:
>>
>> class Foo {
>>
>>   // Methods handled by the user
>>   void one() { }
>>   void two() { }
>>   // ...
>>
>>   mixin GeneratedFoo!();
>>
>> }
>>
>> // far, far away
>> template GeneratedFoo() {
>>
>>   void three() { }
>>   void four() { }
>>
>> }
> 
> You can’t just “monkey-patch” the original class, though. With partial 
> classes you could just inject some additional code to the original class.

The thing is, in C# all pieces of a partial class must be defined in the 
same assembly, and it also must be defined as "partial class". So if you 
can "monkey-patch" a partial class, you could just as easily go to the 
class' source code and inject that additional code.

Partial classes are just a convenience for defining many pieces of a 
class in multiple files.

> 
> I don’t think partial classes fit into D. You can do the same thing via 
> abstract classes and subclassing without altering the original 
> class/interface.

Yes, I don't like much the idea either. I like to have all of a class' 
code in one file. Otherwise, at least for me, it makes it hard to follow 
code, to know where something is defined.



More information about the Digitalmars-d mailing list