我正在使用这个库中的一些代码:https://github.com/Netflix-Skunkworks/rewrite
当我调用它的一个方法时,我会遇到一个IDE错误:
提供的参数不能调用下列任何函数。
目标方法有两个类似的签名:
data class CompilationUnit(...){
fun refactor() = Refactor(this)
fun refactor(ops: Refactor.() -> Unit): Refactor {
val r = refactor()
ops(r)
return r
}
fun refactor(ops: Consumer<Refactor>): Refactor {
val r = refactor()
ops.accept(r)
return r
}
}Kotlin中的呼叫代码:
val unit: CompilationUnit =...
unit.refactor{ tx ->
doSomeThing()
}这个使用lambda的调用在Java中是可以的:
CompilationUnit unit = ....
unit.refactor(tx -> {
doSomeThing()
});发布于 2017-04-13 10:41:40
您可以在Kotlin中修复调用代码:您正在传递一个带有一个参数{ tx -> doSomething() }的lambda,但是传递一个带有接收器的lambda,并且那里没有任何参数。
比较:(Refactor) -> Unit是带有一个参数的函数的类型,而Refactor.() -> Unit是一个带接收器的功能,它不接受参数,而是传递一个Refactor类型的接收方(this)。这些类型有时是可互换的,但lambda并不是在它们之间隐式转换的。
因此,您可以按以下方式调用refactor:
val unit: CompilationUnit = ...
unit.refactor {
doSomeThing() // this code can also work with `this` of type `Refactor`
}或者,要用Consumer<Refactor>调用重载,可以显式地指定:
unit.refactor(Consumer { tx -> doSomething() })显然,隐式山姆转换不可用,因为有几个带有函数参数的重载。
https://stackoverflow.com/questions/43389783
复制相似问题