Skip to content

Instantly share code, notes, and snippets.

@deigote
Created May 30, 2014 10:24
Show Gist options
  • Save deigote/aec2abd2d8ec3d72b66c to your computer and use it in GitHub Desktop.
Save deigote/aec2abd2d8ec3d72b66c to your computer and use it in GitHub Desktop.
Trying to use type checked closures in Groovy, including their parameters
private static File findOuputFile() {
this.<File>logAndReturn('info', { File outputFile -> "Using '$outputFile' as output file" }) {
new File("${System.getProperty('outputDir')}/memolo.html")
}
}
private static <O> O logAndReturn(
String logLevel,
@ClosureParams(ThirdParam.FirstGenericType.class) Closure<String> stringMessageGenerator,
Closure<O> resultProducer
) {
O result = resultProducer()
LoggerFactory.getLogger(this.class).invokeMethod(logLevel, stringMessageGenerator(result))
result
}
// Compiler says: Expected parameter of type java.lang.Object but got java.io.File
// .<File>logAndReturn('info', { File outpu
// ^
@melix
Copy link

melix commented May 30, 2014

Currently the compiler looses the information about explicit type hints (.) so unfortunatly, this cannot work. We're aware of this: https://jira.codehaus.org/browse/GROOVY-6757

Feel free to comment on that issue with your example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment