Kotlin's scoping functions for Groovy.
- Typesafe GScope is compatible with type checking and static compilation.
- Small GScope has no external dependencies.
repositories {
maven {
url 'http://dl.bintray.com/helpermethod/maven'
}
}
dependencies {
compile 'com.github.helpermethod:gscope:0.2.0'
}
<repositories>
<repository>
<id>bintray</id>
<url>http://dl.bintray.com/helpermethod/maven</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.helpermethod</groupId>
<artifactId>gscope</artifactId>
<version>0.2.0</version>
</dependency>
class Person {
String firstName
String lastName
}
def ash = new Person().apply {
firstName = 'Ash'
lastName = 'Williams'
}
apply
calls the specified closure withthis
value as its delegate and returnsthis
.
apply
is used for post-construction initialisation.
def ash = new Person().apply {
firstName = 'Ash'
lastName = 'Williams'
}
apply
may also be used to expose a fluent API for methods that would normally return void
.
class Person {
String firstName
String lastName
def firstName(String firstName) {
apply { this.firstName = firstName }
}
def lastName(String lastName) {
apply { this.lastName = lastName }
}
}
def ash = new Person().firstName('Ash').lastName('Williams')
also
calls the specified closure withthis
as its argument and returnsthis
.
also
is like apply
except that this
becomes the closure's argument instead of its delegate.
Like apply
it's used for initialisation.
def ash = new Person().also {
it.firstName = 'Ash'
it.lastName = 'Williams'
}
also
may also be used to assign calculated values to fields.
class Person {
Person father
List<Person> children
def father(Person father) {
father.also {
this.father = it
it.children += this
}
}
}
run
calls the specified closure withthis
value as its delegate and returns its result.
run
is used for transforming values.
def ashWilliams = new Person(firstName: 'Ash', lastName: 'Williams').run {
"$firstName $lastName"
}
let
calls the specified closure withthis
as its argument and returns its result.
let
is like run
except that this
becomes the closure's argument instead of its delegate.
Like run
it's used for transforming values.
def ashWilliams = new Person(firstName: 'Ash', lastName: 'Williams').let {
"$it.firstName $it.lastName"
}
let
may also be used to execute a block of code when the delegate is non-null.
class PersonUtils {
static def fullName(Person person) {
person?.let {
println(it)
"$it.firstName $it.lastName"
} ?: 'John Doe'
}
}
def ashWilliams = PersonUtils.fullName(ash)
def johnDoe = PersonUtils.fullName(null)