My two cents

Satoshi satoshi at rikarin.org
Tue Oct 24 08:13:10 UTC 2017


On Tuesday, 24 October 2017 at 07:55:49 UTC, Walter Bright wrote:
> On 10/24/2017 12:21 AM, Satoshi wrote:
>> what about this:
>> 
>> ---------- file.d
>> class Foo {
>>    private int bar;
>>    private string text;
>> 
>>    void call() { }
>> }
>> 
>> ----------- file.di
>> 
>> class Foo {
>>    call();
>> }
>> 
>> 
>> 
>> and then I want to inherit from it (I have access to file.di 
>> only)
>> class Bar : Foo { // I cannot due I have misleading 
>> information about size of Foo
>> 
>> }
>
>
> If you don't add data members to Bar, it will work. If you do 
> add data members, then the compiler needs to know the data 
> members in Foo. This is the same in C++ .h files.
>
> You could specify Foo as an interface,
>
>   https://dlang.org/spec/interface.html
>
> and then Bar can inherit from Foo without needing any knowledge 
> of Foo's data members.


But it's quite useless to me.


i.e.
class View { }
class Button : View { }

I want to let users to inherit from View or Button and let them 
customize the elements.
This can be done just by PIMPL.

But doing PIMPL inheritance inside one library is pain in the a$$.

-------------------- view.d ------------------
class View {
   private ViewData _;

   package class ViewData {
     int a, b, c;
   }

   this() {
     initData();
   }

   protected void initData() {
     if (!_) _ = new ViewData;
   }

   void render() { }
}


----------------------- button.d -----------
class Button : View {
   package class ButtonData : ViewData {
     string stuff;
   }

   protected override void initData() {
     if (!_) _ = new ButtonData;
   }

   void simulateClick() { }
}


and headers
------------- view.di ------------
class View {
   private void* _;

   this();
   void render();
}

------------- button.di ---------------
class Button : View {
   void simulateClick();
}



But the worst part is that I need to hold di in sync to d files 
manually, so there is not any advantages over C++'s header files.


More information about the Digitalmars-d mailing list