https://github.com/helpermethod/gscope
Kotlin's scoping functions for Groovy.
https://github.com/helpermethod/gscope
extension-module groovy type-safe
Last synced: about 2 months ago
JSON representation
Kotlin's scoping functions for Groovy.
- Host: GitHub
- URL: https://github.com/helpermethod/gscope
- Owner: helpermethod
- License: apache-2.0
- Created: 2018-03-07T20:53:28.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-05T13:53:47.000Z (almost 7 years ago)
- Last Synced: 2025-02-05T03:36:35.585Z (3 months ago)
- Topics: extension-module, groovy, type-safe
- Language: Groovy
- Homepage:
- Size: 72.3 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GScope
Kotlin's scoping functions for Groovy.
## Features
* **Typesafe** GScope is compatible with type checking and static compilation.
* **Small** GScope has no external dependencies.## Setup
### Gradle
```groovy
repositories {
maven {
url 'http://dl.bintray.com/helpermethod/maven'
}
}dependencies {
compile 'com.github.helpermethod:gscope:0.2.0'
}
```### Maven
```xml
bintray
http://dl.bintray.com/helpermethod/maven
com.github.helpermethod
gscope
0.2.0```
## Quickstart
```groovy
class Person {
String firstName
String lastName
}def ash = new Person().apply {
firstName = 'Ash'
lastName = 'Williams'
}
```## API
### apply
> `apply` calls the specified closure with `this` value as its delegate and returns `this`.
`apply` is used for post-construction initialisation.
```groovy
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`.
```groovy
class Person {
String firstName
String lastNamedef firstName(String firstName) {
apply { this.firstName = firstName }
}def lastName(String lastName) {
apply { this.lastName = lastName }
}
}def ash = new Person().firstName('Ash').lastName('Williams')
```### also
> `also` calls the specified closure with `this` as its argument and returns `this`.
`also` is like `apply` except that `this` becomes the closure's argument instead of its delegate.
Like `apply` it's used for initialisation.
```groovy
def ash = new Person().also {
it.firstName = 'Ash'
it.lastName = 'Williams'
}
````also` may also be used to assign calculated values to fields.
```groovy
class Person {
Person father
List childrendef father(Person father) {
father.also {
this.father = it
it.children += this
}
}
}
```### run
> `run` calls the specified closure with `this` value as its delegate and returns its result.
`run` is used for transforming values.
```groovy
def ashWilliams = new Person(firstName: 'Ash', lastName: 'Williams').run {
"$firstName $lastName"
}
```### let
> `let` calls the specified closure with `this` 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.```groovy
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.
```groovy
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)
```