Created
May 30, 2014 10:24
-
-
Save deigote/aec2abd2d8ec3d72b66c to your computer and use it in GitHub Desktop.
Trying to use type checked closures in Groovy, including their parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
// ^ |
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
As a test, I changed line 9 to have ThirdParam.class and, as expected, it said it was expecting a Closure but received a File.
But when I try with the generic, which is set to be File, it's says it expects an Object.
Code was tested against Groovy 2.3.2.