相关疑难解决方法(0)

Kotlin:使Java函数可调用中缀

试图将powBigInteger类中的函数作为具有相同名称的中缀函数提供.

问题是现在中pow缀运算符递归调用自身.

是否可以使用与函数同名的中缀运算符使Java函数可调用?

package experiments

import java.math.BigInteger

infix fun BigInteger.pow(x: BigInteger): BigInteger {
    return this.pow(x);
}

fun main(args : Array<String>) {
    val a = BigInteger("2");
    val b = BigInteger("3");

    println(a + b)
    println(a pow b)
}
Run Code Online (Sandbox Code Playgroud)

原因:

Exception in thread "main" java.lang.StackOverflowError
    at kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull(Intrinsics.java:126)
    at experiments.KotlinTestKt.pow(KotlinTest.kt)
    at experiments.KotlinTestKt.pow(KotlinTest.kt:6)
Run Code Online (Sandbox Code Playgroud)

kotlin

3
推荐指数
1
解决办法
414
查看次数

标签 统计

kotlin ×1