看板java
标 题Re: [情报] Java 即将加入 Closure
发信站SayYA 资讯站 (Sat Sep 2 17:29:34 2006)
转信站ptt!ctu-reader!news.nctu!SayYa
※ 引述《[email protected] (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 的概念.
--
[email protected] [email protected]
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