delegates and functions

drug drug2004 at bk.ru
Sat Jun 9 20:55:21 UTC 2018


On 09.06.2018 23:39, OlegZ wrote:
> On Saturday, 9 June 2018 at 20:03:15 UTC, OlegZ wrote:
> 
>>> auto hz = (string s) => { writeln( s ); return cast( int )s.length; }
>> How I should to write lambda of type "int delegate( string )?
> 
> I found one way:
>> auto hz = delegate int( string s ) { writeln( s ); return cast( int 
>> )s.length; };
> 
> but still don't understand why
>> return cast( int )s.length;
> at topmost line returns "int delegate()" not just int?
just remove `=>`, either:
```
auto hz = (string s) { writeln( s ); return cast( int )s.length; }
```
or
```
auto hz = (s) => writeln( s );
```

In fact using `=>` you really define a function returning delegate.

```
auto hz = (string s) => { writeln( s ); return cast( int )s.length; }
```

is equivalent to

```
auto hz = (string s) {
	return () {writeln( s ); return cast( int )s.length;}
}
```


More information about the Digitalmars-d-learn mailing list