My two cents

Satoshi satoshi at rikarin.org
Wed Oct 25 07:39:08 UTC 2017


On Tuesday, 24 October 2017 at 20:36:47 UTC, Walter Bright wrote:
> On 10/24/2017 3:36 AM, Satoshi wrote:
>> Can you provide an example?
>
> I'd start with https://dlang.org/spec/interface.html
>
> You'll see the same thing with Windows COM programming, and 
> using interfaces in Java.
>
> ---- view.di ----
> interface View {
>     void render();
> }
> View createView();
>
> ---- button.di ----
> import view;
> interface Button : View {
>     void simulateClick();
> }
> Button createButton();
>
> ---- closed_src_library.d ----
> import view, button;
> private class LibraryView : View {
>    int hidden_data;
>    void render() { do something with hidden_data }
> }
>
> private class LibraryButton : Button {
>     int hidden_data;
>     void simulateClick() { .... }
> }
>
> View createView() {
>     return new LibraryView();
> }
>
> View createButton() {
>     return new LibraryButton();
> }
>
> --- my_extension.d ---
> import view, button;
> class ExtendView : View {
>     View base;
>     this() {
>         base = createView();
>     }
>     void render() {
>         base.render();
>         ... do my rendering ...
>     }
> }

Thanks, but I see there 3 problems:
1. this example enforce users to use composition instead of 
inheritance when they wants to create custom descendant.
2. multiple dispatch levels. Extending the ExtendView in another 
lib we get triple dispatch for each method.
3. 1:1 methods mapping is required? or can be there used some 
sort of aliasing?

interface View {
   void render();
   // another hundred methods
}

class ControlView : View {
   View parent;

   void render() { parent.render(); }
}

class ButtonView : View {
   ControlView parent;

   void render() { parent.render(); } // triple dispatch
}




More information about the Digitalmars-d mailing list