View mode: basic / threaded / horizontal-split · Log in · Help
November 09, 2012
Re: delegate bug?
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;
}
November 09, 2012
Re: delegate bug?
On Friday, 9 November 2012 at 20:22:40 UTC, Timon Gehr wrote:
> The following works. You can also use a (scoped) delegate in 
> order to allow the caller to close over his context.

Wow! Great! Thanks.
But I can't understend this syntax:
x=>x.draw()


Where i can read about it?
November 09, 2012
Re: delegate bug?
I have found it. This is shorthand lambda syntax.
November 09, 2012
Re: delegate bug?
On 2012-11-09 21:31, Jack Applegame wrote:

> Wow! Great! Thanks.
> But I can't understend this syntax:
> x=>x.draw()
>
>
> Where i can read about it?

It's a delegate literal, or also known as a lambda.

http://dlang.org/expression.html#Lambda

-- 
/Jacob Carlborg
November 09, 2012
Re: delegate bug?
On 11/09/2012 09:31 PM, Jack Applegame wrote:
> On Friday, 9 November 2012 at 20:22:40 UTC, Timon Gehr wrote:
>> The following works. You can also use a (scoped) delegate in order to
>> allow the caller to close over his context.
>
> Wow! Great! Thanks.
> But I can't understend this syntax:
> x=>x.draw()
>
>
> Where i can read about it?

It is a lambda function literal. It is as if you did

static void action(Figure x){
    return x.draw();
}
doAction(figures, &action);

which can also be written in any of the following ways. Because the 
types are mandated by the function that is called, you can leave them out:

doAction(figures, function void(Figure x){ return x.draw(); }); // 
function literal
doAction(figures, function(x){ return x.draw(); }); // infer types
doAction(figures, (x){ return x.draw(); }); // infer 'function'
doAction(figures, (x)=>x.draw()); // convenient lambda syntax
doAction(figures, x=>x.draw());   // also without parentheses


It is documented here (scroll up a bit, Lambda does not seem to have an 
anchor)
http://dlang.org/expression.html#FunctionLiteral

In this case, we used the feature to implement a member function 
pointer, but there are many more applications that such a shorthand 
syntax makes convenient.

It might generally be helpful for your programming skills to get 
accustomed to functional programming a bit. So in case you are 
interested, I suggest you consult your search engine of choice about the 
subject. You could eg. look into Scheme and/or Haskell and try to 
reproduce some of the examples also in D.
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home