On 04/18/2012 03:50 PM, Mirko Pilger wrote: >> I want to know what is most interesting for me: delegates or functions. >> I consulted sources but none say the practical consequences of such >> election. > > in addition to what john said: regarding _function literals_ the > difference is by using a delegate instead of a function you have access > to the enclosing frame, e.g. variables in the same scope. > > the following code doesn't compile with the error "[...] cannot access > frame of [...]": > > int y; > auto fn= function(int x) {return x+y;}; > > if you change this to: > > int y; > auto fn= delegate(int x) {return x+y;}; > > it compiles without error.
Additionally, thanks to a recent bug fix, omitting 'function' and 'delegate' will deduce the right one. Both of the following are delegates because they access the variable 'y' from the enclosing scope:
auto fn_1 = (int x) {return x+y;}; auto fn_2 = (int x) => x + y; The latter syntax doesn't help much in this context but still... Ali