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 | |
// ^ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.