这是老消息...已经"过期"一个多星期了.
刚才去收集D语言的动向消息时才看到,突然觉得...好感动(?)
原本还在抱怨D语言缺乏完整的闭包,只有个半调子的"动态闭包",结果现在就有了.
不得不说,这真是种神奇的语言...
官网上关于2.007的新增特性的描述:
引用
New/Changed Features
- Functors now supported by std.traits.ReturnType().
- Transitive const now leaves invariants intact in the tail.
- Added overloadable unary * operation as opStar().
- Full closure support added.
- Data items in static data segment >= 16 bytes in size are now paragraph aligned.
马上来看看下面这段代码的运行是否正常:(表现闭包的经典简短代码...)
int delegate(int) add(int lhs) {
int foo(int rhs) {
return lhs + rhs;
}
return &foo;
}
void main(char[][] args) {
int delegate(int) addFive = add(5);
int twelve = addFive(7);
printf("5 + 7 is %d", twelve);
}
引用
K:\D\workspace\test>test
5 + 7 is 12
很好很强大...我真是没什么好抱怨的了,几乎所有的抱怨看来都能有被解决的一天 (泪
当然上面的add函数还可以进一步被简化如下:
int delegate(int) add(int lhs) {
return delegate int (int rhs) {
return lhs + rhs;
};
}
既然我们不会在add内调用这个内部函数,当然也没必要给它一个名字.能用匿名函数字面量的话自然是最好 ^ ^
Cheers