delegates and functions

drug drug2004 at bk.ru
Sun Jun 10 13:25:17 UTC 2018


On 10.06.2018 12:21, OlegZ wrote:
> On Saturday, 9 June 2018 at 22:28:22 UTC, Ali Çehreli wrote:
>> There is some explanation at the following page, of how the lambda 
>> syntax is related to the full syntax:
>>   http://ddili.org/ders/d.en/lambda.html#ix_lambda.=%3E
> 
> copy rect from article as image 
> https://i.gyazo.com/c23a9139688b7ed59fbe9c6cdcf91b93.png >
> well, I can to remember that lambda () => { return ..; } returns another 
> implicit lambda

you shouldn't remember it. you need to understand that `a=>2*a` is short 
form of `(a) { return 2a; }. So using `a=>{ return 2*a; }` you get
```
(a) { return (){ return 2*a; }; }
```
i.e. a function returning delegate.
```
a => { return 2*a; }
   /\  \           /
   ||   \         /
   ||    \       /
   ||     \     /
   ||      \   /
  This is   \ /
  function   \  This is definition of delegate
  definition  \

	so you have a function that returns delegate.

```
it's like
a => a => 2*a;
or
(a){ return () { return 2*a; }

just first version is much simpler and namely convenience is the reason 
of this syntax I guess. >
> can somebody tell me please what was the reason that lambda (blah) => { 
> return value + 1; } actionally returns another lambda that returns 
> value+1? what case it can be usefull? why Dlang gives me lambda of 
> lambda where I want lambda written with "=> { return"?
> I am expecting some magic and simple trick with lambdas.

You misuse two different form of the same. The form with "=>" is very 
convenient if expression is short, in other cases the form `(){}` is 
suited better.


More information about the Digitalmars-d-learn mailing list