Re: [情報] Java 即將加入 Closure

看板java作者時間19年前 (2006/09/02 18:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串16/16 (看更多)
※ 引述《godfat.bbs@ptt.cc (godfat 真常)》之銘言: > ※ 引述《jtmh (Believing is seeing! ^^)》之銘言: > :   有喔,它是先引入 function types 與 local functions, > :   然後再把 local functions 簡化為 anonymous functions (closures) 的, > :   以下是它的範例: > : public static void main(String[] args) { > : int plus2(int x) { return x+2; } > : int(int) plus2b = plus2; > : System.out.println(plus2b(2)); > : } > 這個不是 function pointer 的概念,沒有說明如何包裝物件方法 > 假設: > void print( int() f ){ > System.out.println( f() ); > } > 下面這要怎麼做? > Integer i = new Integer(10); > print( i.hashCode ); > 還是會變成這樣: > print( int(){ return i.hashCode(); } ); > 這就是多此一舉了 > 不過我覺得 Java 的設計理念就是要你多打一點字 > 為了省幾個字換來更複雜的東西,似乎一直是 Java 避免的 > 也許 closure 就已經是很大的讓步了也說不定 這不叫 closure, 這叫 lambda ... closure 確實惟M lambda 配合使用, 但 closure 的概念和 lambda 是不同的。 void print(int() f) { System.out.println( f() ); } void foo(void) { int x = 0; int i; for(i = 0; i < 100; i++) { print( int(){ x = x + i; return x; }; } } ==> 0 1 3 6 .. .. .. 這一段 code 才真的表達出 closure 的功能... 之前的 code 都是在表現 lambda 的特色. from http://en.wikipedia.org/wiki/Closure_%28computer_science%29 ```In programming languages, a closure is a function that refers to free variables in its lexical context.''' 這裏很明白的指出 free variables in its lexical context 的概念. -- thinker@branda.to thinker.li@gmail.com http://heaven.branda.to/~thinker/GinGin_CGI.py -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: 219-84-62-240-adsl-tpe.dynamic.so-net.net.tw
文章代碼(AID): #14-LPa00 (java)
討論串 (同標題文章)
文章代碼(AID): #14-LPa00 (java)