digitalmars.D.learn - anonymous function/deleget usage
- Sam Hu (7/7) Nov 12 2009 How can I reach something like below code:
- Don (7/16) Nov 12 2009 You need to call the delegate you've made.
- Steven Schveighoffer (4/18) Nov 12 2009 Also, don't forget you can refer to variables in the enclosing function:
- bearophile (5/6) Nov 12 2009 You can also write:
- Ary Borenszweig (3/12) Nov 12 2009 Shorter:
- Sam Hu (12/13) Nov 12 2009 I missed this key point.
How can I reach something like below code: int a=1; int b=2; int c=(int a,int b){ return a+b;} writefln("Result:%d",c); Thanks in advance.
Nov 12 2009
Sam Hu wrote:How can I reach something like below code: int a=1; int b=2; int c=(int a,int b){ return a+b;} writefln("Result:%d",c); Thanks in advance.You need to call the delegate you've made. int a=1; int b=2; int c=(int a,int b){ return a+b;}(a,b); writefln("Result:%d",c);}
Nov 12 2009
On Thu, 12 Nov 2009 10:21:44 -0500, Don <nospam nospam.com> wrote:Sam Hu wrote:Also, don't forget you can refer to variables in the enclosing function: int c = (){return a + b;}(); -SteveHow can I reach something like below code: int a=1; int b=2; int c=(int a,int b){ return a+b;} writefln("Result:%d",c); Thanks in advance.You need to call the delegate you've made. int a=1; int b=2; int c=(int a,int b){ return a+b;}(a,b); writefln("Result:%d",c);}
Nov 12 2009
Steven Schveighoffer:int c = (){return a + b;}();You can also write: int c = {return a + b;}(); Bye, bearophile
Nov 12 2009
bearophile wrote:Steven Schveighoffer:Shorter: int c = a + b;int c = (){return a + b;}();You can also write: int c = {return a + b;}(); Bye, bearophile
Nov 12 2009
Don Wrote:You need to call the delegate you've made.I missed this key point. So to summary: int a=1; int b=2; 1.nested function; 2.int c=(int a,int b){return a+b;}(a,b); 3.int c=(int,int){return a+b;}(a,b); 4.int c=(){return a+b;}(); 5.int c={return a+b;}(); How come the last one is legal? Thank you all for all your help!
Nov 12 2009
Hello Sam,Don Wrote:If the function has no args the first () can be dropped. If the return type can be inferred, that can be dropped. Nested functions can access vars in the outer function.You need to call the delegate you've made.I missed this key point. So to summary: int a=1; int b=2; 1.nested function; 2.int c=(int a,int b){return a+b;}(a,b); 3.int c=(int,int){return a+b;}(a,b); 4.int c=(){return a+b;}(); 5.int c={return a+b;}(); How come the last one is legal?Thank you all for all your help!
Nov 12 2009
BCS Wrote:If the function has no args the first () can be dropped. If the return type can be inferred, that can be dropped. Nested functions can access vars in the outer function.Got it.Thanks a lot!
Nov 12 2009