Skip to content

Instantly share code, notes, and snippets.

@melix
Created November 2, 2014 10:35
Show Gist options
  • Save melix/9ed22f8c2cc9698767bc to your computer and use it in GitHub Desktop.
Save melix/9ed22f8c2cc9698767bc to your computer and use it in GitHub Desktop.
DelegatesTo + generic
import groovy.transform.CompileStatic
public <T> T configure(@DelegatesTo.Target Class<T> clazz, @DelegatesTo(genericTypeIndex = 0) Closure<Void> conf) {
def result = clazz.newInstance()
result.with(conf)
result
}
class Person {
String name
int age
}
@CompileStatic
void testIt() {
def p = configure(Person) {
name = 'Bob'
age = 42
}
println(p)
}
testIt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment