有没有办法将 java 方法声明为 kotlin 的中缀
Is there a way to declare a java method as infix for kotlin
有没有办法从 java class 中声明一个方法,以便它可以作为 infix
函数从 kotlin 中调用,就像这样:
public class Foo {
public void doFoo (String bar) {}
}
然后从 kotlin 文件
foo doFoo "bar"
Since Java has no way of marking methods for which it makes sense to use the operator syntax, Kotlin allows using any Java methods with the right name and signature as operator overloads and other conventions (invoke()
etc.) Calling Java methods using the infix call syntax is not allowed.
您可以添加 infix
扩展以从 Kotlin 获取该语法:
infix fun Foo.doFoo(bar: String) {
return doFoo(bar)
}
有没有办法从 java class 中声明一个方法,以便它可以作为 infix
函数从 kotlin 中调用,就像这样:
public class Foo {
public void doFoo (String bar) {}
}
然后从 kotlin 文件
foo doFoo "bar"
Since Java has no way of marking methods for which it makes sense to use the operator syntax, Kotlin allows using any Java methods with the right name and signature as operator overloads and other conventions (
invoke()
etc.) Calling Java methods using the infix call syntax is not allowed.
您可以添加 infix
扩展以从 Kotlin 获取该语法:
infix fun Foo.doFoo(bar: String) {
return doFoo(bar)
}