Skip to content

Instantly share code, notes, and snippets.

@AarjavP
Created November 4, 2016 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AarjavP/d1b5b0d314e33cb015f09e1afb87ecaa to your computer and use it in GitHub Desktop.
Save AarjavP/d1b5b0d314e33cb015f09e1afb87ecaa to your computer and use it in GitHub Desktop.
package net.phly.groovy
import groovy.transform.CompileStatic
import groovy.transform.stc.ClosureParams
import groovy.transform.stc.FromAbstractTypeMethods
import groovy.transform.stc.SimpleType
@CompileStatic
class ClosureParamTest {
abstract class Foo {
abstract void firstSignature(int x, int y)
abstract void secondSignature(String str)
}
static void doIt(@ClosureParams(value = SimpleType, options = ['java.lang.String']) Closure cl) {
// ..
}
static void doSomething(@ClosureParams(value=FromAbstractTypeMethods, options=["Foo"]) Closure cl) {
// ..
}
static void doSomethingDifferent(@ClosureParams(value=FromAbstractTypeMethods, options=["net.phly.groovy.ClosureParamTest.Foo"]) Closure cl) {
// ..
}
public static void main(String[] args) {
doIt { s -> s.toUpperCase() } //compiles
doSomething { a, b -> a + b } //'works' but does not recognise a and b as int
doSomething { s -> s.toUpperCase() } //does not compile
doSomethingDifferent { a, b -> a + b } //'works' but does not recognise a and b as int
doSomethingDifferent { s -> s.toUpperCase() } //does not compile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment