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
精彩评论