|
演员Christopher Walken曾在周末夜现场(Saturday Night Live)上演了一出喜剧,他说要“拳打南山猛虎、脚踢北海苍龙”。同样,Groovy团队也听到了人们的呼声,要求加入更多的特性,就在最近他们发布了该获得大奖的语言的1.6版。其发布声明宣称该版本拥有众多的新特性,包括:
- 运行时性能的巨大改进
- 多路赋值——if/else和try/catch块中可选的return语句
- AST转换及各种转换注解,如@Singleton、@Lazy、@Immutable、@Delegate及助手
- Grape模块和依赖系统及其@Grab转换
- 各种改进的Swing builder,这要归功于Swing / Griffon(http://griffon.codehaus.org)团队
- 几个Swing控制台的改进
- 集成JMX builder
- 内建的JSR-223脚本引擎
- 改进的各种元编程,像是EMC DSL、甚至是针对POJO的单实例元类及运行时掺元(mixin)
该版本的一个主要关注点就是性能,Groovy团队说已经将性能提高了150%到460%。另一个特性就是对JMX builder提供了官方的集成,这又一次证明了社区对Groovy改进的巨大推动力。
以下示例展示了该版本的几个新特性,包括多路赋值及AST转换。
// this class' properties are immutable once the object is constructed
@Immutable final class ServerConfig {
String url
int port
}
def getServerInfo() {
['http://home.net', 8080]
}
// attempts to set a property on an Immutable object
def setUrl(config, newUrl) {
try {
config.url = newUrl
}
catch (ReadOnlyPropertyException ex) {
ex
}
}
// multiple assignment
def (url, port) = getServerInfo()
assert url == 'http://home.net'
assert port == 8080
def config = new ServerConfig(url, port)
assert config.url == url
assert config.port == port
// try to change the property on the Immutable object
def result = setUrl(config, 'www.google.com')
// verify the property change failed
assert result instanceof ReadOnlyPropertyException
上面的示例展示了@Immutable AST转换如何以简单的方式创建只读对象。该示例还介绍了新的“多路赋值”特性。请查看Groovy用户指南来了解关于AST转换的更多信息,上面的示例只介绍了@Immutable转换。
既然已经对Groovy 1.6有了一个大概的了解,为何不看看Groovy 1.6的新特性这篇文章呢? 这是Groovy项目经理Guillaume LaForge专门为InfoQ撰写的关于Groovy 1.6的文章,该文并不是泛泛而谈,而是深入剖析了Groovy 1.6的新特性。Guillaume介绍了所有的新特性并提供了大量代码示例来阐述这些新功能。
查看英文原文:Just the Cure, More Groovy
|