I'm having a problem converting gradle scripts to ...
# gradle
e
I'm having a problem converting gradle scripts to kotlin regarding of closure: I have this method signature in Groovy:
static copyAndRenameChildFiles(File sourceDir, Closure<String> filenameTransform)
and I'm trying to call from a kotlin script, I tried to use `closureOf`like this:
val copyDir = FileUtils.copyAndRenameChildFiles(svgSrcDir, closureOf<String> { this.replace("-", "_") })
but I'm getting the error:
Type mismatch: inferred type is Closure<Any?> but Closure<String!>! was expected
the original call on Groovy was:
File copyDir = FileUtils.copyAndRenameChildFiles(svgSrcDir, { String filename -> filename.replace("-", "_") })
s
The type parameter of
Closure
is its return type, whereas the type parameter of
closureOf
is the input type 😬. Try
KotlinClosure1<String, String>({ this.replace("-", "_") })
, which lets you specify both type parameters.
e
it works, you are the best.... thanks
v
Or if you can modify
copyAndRenameChildFiles
, use
Transformer
instead of
Closure
Then you do not need closure-tricks in Kotlin but can just use a closure from Groovy and a lambda from Kotlin