Closure
Use Cases for Closures
separating iteration logic from what to do on each iteration
handling resources,
open
do the job // in a closure, as parameter
close
Implicit Variable of Closure
it default parameter
this enclosing class
owner enclosing object, class or closure
delegate same as owner, but changeable
closure without parameters
{ -> ... }
closure with one parameter
{ ... }, { x -> ... }
Scope of a closure
a closure must remember original context frame. it is closed under declaration context.
Return from a closure or method
end return
the value of last expression is returned.
prematurely return with return keyword
Groovy Control Structure
Groovy Truth
Boolean Test: evaluate an expression to be true or false
any non-void type can be evaluated as Boolean.
to be true
non empty, non zero, non null
Typo, forget a =
if(x = 1) is not allowed in groovy.
if(1 == x) is safe than if(x == 1)
Loop
an object can be iterable.
assert 如果没有assert, code一样产生够用的错误信息,那么就不需要assert
assert can clarify the code intents. runnable comment!
Return Value
method with void do not return a value
closure always return a value, null if no value
OO in Groovydynamic, object oriented and scripting
classes and
scripts definition
script’s binding
undeclared variable
transfer variables between Script and Calller
default visibility of fields ==> property definition
default visibility of methods ==> public
Reference a field
Class and Map has some similiarity.
obj.field
obj[’field’]
override get and set methods
Parameter Type
optional, if not specified, then means Object.
Groovy’s method dispatch
GroovyObject.invokeMethod(name, params[])
Safe dereferencing with ?. operator
protected from NPE
null?.foo() no foo called, return null
Constructor called by
1. new ClassName(...)
2. as with list
3. implicit coercion with list, implicit construction
named parameters for default constructor
available if no constructor is defined
Type aliasing
import ... as ..
Typing
Interface
Design by Contract
Duck Typing, Dynamic Typing, checked by tests
Multimethods
method look up take the
dynamic type of arguments into account.
method dispatch at run time(Groovy) or compile time(Java)
GroovyBeans
obj.property mapped to obj.getProperty() or obj.setProperty(value) method
dot-@ operator
obj.@field access directly the field
event in groovy bean
btn.actionPerformed = { ... }
groovy通过bean introspection确定是否为添加一个Listener,如果是, 生成一个ClosureListener,
a proxy implementation of the required listener interface,调用后面的closure,添加
GPath
objs.p1,p2*.p3
Spread Operator *
*list 打散
mix-in with use dynamic 增加method
use(category) {
....
}
category中定义static methods, use后, method的第一个argument的类得到一个新的method
Groovy's Meta-Object Protocoll(MOP)
how Groovy performs its magic?
interception points
All classes programmed in Groovy are constructed by the
GroovyClassGenerator, such that it implements
GroovyObject interface.
Method Dispatching
this.invokeMethod()
getMetaClass().invokeMethod()
MetaClassRegistry.getMetaClass(this.class).invokeMethod()
Usability intercept
relay
pretend