On 11/09/2012 09:13 PM, Jack Applegame wrote:
Ok. I will try to explain what exactly i need.

...

The following works. You can also use a (scoped) delegate in order to allow the caller to close over his context.

import std.stdio;

class Figure {
    void draw(){}
    void erase(){}
}

class Circle : Figure {
    override void draw() { writeln("drawing circle"); }
    override void erase() { writeln("erasing circle"); }
}

class Square : Figure {
    override void draw() { writeln("drawing square"); }
    override void erase() { writeln("erasing square"); }
}

class Triangle : Figure {
    override void draw() { writeln("drawing triangle"); }
    override void erase() { writeln("erasing triangle"); }
}

void main() {
    Figure[] figures;
    createFigures(figures);
    doAction(figures, x=>x.draw());
    doAction(figures, x=>x.erase());
}

void doAction(Figure[] figures, void function(Figure) action) {
    foreach(Figure figure; figures) {
        action(figure);
    }
}

void createFigures(ref Figure[] figures) {
    figures ~= new Circle;
    figures ~= new Square;
    figures ~= new Triangle;
}


Reply via email to