项目作者: helpermethod

项目描述 :
Kotlin's scoping functions for Groovy.
高级语言: Groovy
项目地址: git://github.com/helpermethod/gscope.git
创建时间: 2018-03-07T20:53:28Z
项目社区:https://github.com/helpermethod/gscope

开源协议:Apache License 2.0

下载


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

  1. repositories {
  2. maven {
  3. url 'http://dl.bintray.com/helpermethod/maven'
  4. }
  5. }
  6. dependencies {
  7. compile 'com.github.helpermethod:gscope:0.2.0'
  8. }

Maven

  1. <repositories>
  2. <repository>
  3. <id>bintray</id>
  4. <url>http://dl.bintray.com/helpermethod/maven</url>
  5. </repository>
  6. </repositories>
  7. <dependency>
  8. <groupId>com.github.helpermethod</groupId>
  9. <artifactId>gscope</artifactId>
  10. <version>0.2.0</version>
  11. </dependency>

Quickstart

  1. class Person {
  2. String firstName
  3. String lastName
  4. }
  5. def ash = new Person().apply {
  6. firstName = 'Ash'
  7. lastName = 'Williams'
  8. }

API

apply

apply calls the specified closure with this value as its delegate and returns this.

apply is used for post-construction initialisation.

  1. def ash = new Person().apply {
  2. firstName = 'Ash'
  3. lastName = 'Williams'
  4. }

apply may also be used to expose a fluent API for methods that would normally return void.

  1. class Person {
  2. String firstName
  3. String lastName
  4. def firstName(String firstName) {
  5. apply { this.firstName = firstName }
  6. }
  7. def lastName(String lastName) {
  8. apply { this.lastName = lastName }
  9. }
  10. }
  11. 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.

  1. def ash = new Person().also {
  2. it.firstName = 'Ash'
  3. it.lastName = 'Williams'
  4. }

also may also be used to assign calculated values to fields.

  1. class Person {
  2. Person father
  3. List<Person> children
  4. def father(Person father) {
  5. father.also {
  6. this.father = it
  7. it.children += this
  8. }
  9. }
  10. }

run

run calls the specified closure with this value as its delegate and returns its result.

run is used for transforming values.

  1. def ashWilliams = new Person(firstName: 'Ash', lastName: 'Williams').run {
  2. "$firstName $lastName"
  3. }

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.

  1. def ashWilliams = new Person(firstName: 'Ash', lastName: 'Williams').let {
  2. "$it.firstName $it.lastName"
  3. }

let may also be used to execute a block of code when the delegate is non-null.

  1. class PersonUtils {
  2. static def fullName(Person person) {
  3. person?.let {
  4. println(it)
  5. "$it.firstName $it.lastName"
  6. } ?: 'John Doe'
  7. }
  8. }
  9. def ashWilliams = PersonUtils.fullName(ash)
  10. def johnDoe = PersonUtils.fullName(null)