Kotlin无法转换gradle';一个lambda的动作类
因此,虽然这是一个针对gradle特定问题的kotlin dsl,但我认为它总体上适用于kotlin语言本身,所以我不打算使用该标记 在gradle API中,类Kotlin无法转换gradle';一个lambda的动作类,gradle,kotlin,gradle-kotlin-dsl,Gradle,Kotlin,Gradle Kotlin Dsl,因此,虽然这是一个针对gradle特定问题的kotlin dsl,但我认为它总体上适用于kotlin语言本身,所以我不打算使用该标记 在gradle API中,类动作定义为: @HasImplicitReceiver public interface Action<T> { /** * Performs this action against the given object. * * @param t The object to perform
动作定义为:
@HasImplicitReceiver
public interface Action<T> {
/**
* Performs this action against the given object.
*
* @param t The object to perform the action on.
*/
void execute(T t);
}
哪个更好,但它仍然没有解决。现在指定它:
val x = Action<String> { input -> ... }
val x=Action{input->…}
给出以下错误无法推断输入类型
,应无参数
。有人能帮我做些什么吗?你需要用类名引用函数,比如:
val x: Action<String> = Action { println(it) }
val x:Action=Action{println(it)}
这是因为gradle中的操作
类被注释为。从文件中:
将SAM接口标记为lambda表达式/闭包的目标,其中单个参数作为调用的隐式接收器传递(此
在Kotlin中,在Groovy中委托
),就好像lambda表达式是参数类型的扩展方法一样
(强调矿山)
因此,下面的编译很好:
val x = Action<String> {
println(">> ${this.trim()}")
}
val x=操作{
println(“>>${this.trim()}”)
}
你甚至可以只写${trim()}
并省略前面的这个
。所有这些对我来说都很好。也许是一个旧的Kotlin插件版本?所以这真的很奇怪,但不知何故,操作{println(this)}
似乎可以工作。我不知道为什么lambda被视为一个具有接收器而不是单个参数的对象,但至少在gradle构建的上下文中,这似乎是答案。但我不希望编译器对gradle脚本的行为有所不同?
val x = Action<String> {
}
val x = Action<String> { input -> ... }
val x: Action<String> = Action { println(it) }
val x = Action<String> {
println(">> ${this.trim()}")
}