开发者

groovy closure inheritance

开发者 https://www.devze.com 2023-04-05 06:21 出处:网络
开发者_StackOverflow社区How does inheritance work in groovy for closures?Is there anything special to be aware of?My application is to extend a plugin controller, that I need to leave alone should any
开发者_StackOverflow社区

How does inheritance work in groovy for closures? Is there anything special to be aware of? My application is to extend a plugin controller, that I need to leave alone should any updates come in for it.


Closure inheritance doesn't make much sense (in the way we tend to use them anyway). A closure in practice is an instance of the Closure class. If we created subclasses of Closure then we could subclass those, but we don't. For example in controllers, we define actions as inline instances, e.g.

def list = {
...
}

These are treated like methods in that we can call list(), but that's just syntactic sugar for list.call(), since call() is an instance method of the Closure class.

In Grails 2.0 the preferred approach to creating controller actions is to use methods, although closures are still supported for backwards compatibility. One of the primary reasons for this switch is to support overloading and overriding, which isn't possible (or at least practical) with inline closures. You can define a closure instance in a subclass with the same name as a base class instance, but you can't call super.list() since it will result in a StackOverflowError

0

精彩评论

暂无评论...
验证码 换一张
取 消