about lambdas
Maxim Fomin
maxim at maxim-fomin.ru
Wed Jan 2 13:12:32 PST 2013
On Wednesday, 2 January 2013 at 21:00:10 UTC, Michael wrote:
> R With(I, R)(I o, R function (I) fun)
> {
> static if(isAssignable!(I, typeof(null)))
> return o is null ? null : fun(o);
> else
> return fun(o);
> }
>
> class Person
> {
> private
> {
> string _name;
> Address _address;
> }
>
> @property
> {
> string name() { return _name; }
> void name(string v) { _name = v; }
>
> Address address() { return _address; }
> void address(Address v) { _address = v; }
> }
> }
>
> in main function
> ----------------
> foreach(p; persons)
> p.With((Person x) => x.address); // works
>
> but
> ----------------
> foreach(p; persons)
> p.With(x => x.address); // error
>
> nullcheck.d(89): Error: template maybe.With does not match any
> function template
> declaration. Candidates are:
> maybe.d(20): maybe.With(I, R)(I o, R function(I) fun)
> nullcheck.d(89): Error: template maybe.With(I, R)(I o, R
> function(I) fun) cannot
> deduce template function from argument types !()(Person,void)
>
>
> Why?
The first one is a lambda function, the second one is a lambda
template. Templates have type void.
More information about the Digitalmars-d
mailing list