Delegates and classes for custom code.

arturg var.spool.mail700 at gmail.com
Wed Apr 18 02:33:00 UTC 2018


On Wednesday, 18 April 2018 at 01:58:40 UTC, arturg wrote:
>
> is it this what you want?
>
>    class A
>    {
>        int a;
>        void delegate() onDraw;
>
>        this(void delegate() dg)
>        {
>            onDraw = dg;
>        }
>
>        void drawText(string s)
>        {
>            s.writeln;
>        }
>    }
>
>    void main()
>    {
>         A a;
>         a = new A((){ a.a = 5; a.drawText("test"); "drawing 
> all".writeln; });
>     }
>
> but if you do A a = new A((){ a.a = 5; ...});
> the dg cant capture 'a' yet.
> so maybe it would be better to just do:
> A a = new A;
> a.onDraw = (){ a.drawText("test"); "draw rest".writeln; };

ah i see bauss already wrote the same.

some other aproach could be:

class Base
{
     final void draw()
     { drawSelf(); }

     abstract void drawSelf();
}

class A : Base
{
     override void drawSelf()
     { ... }
}


More information about the Digitalmars-d-learn mailing list