2

我是 Kotlin 的真正 n00b 并且刚刚开始使用它的demo

问题解决方案很简单,但有一个错误

private fun assertEquals<T>(actual : T?, expected : T?, message : Any? = null) {
    if (actual != expected) {
        errors++
        println("Test failed")
        val trace = Thread.currentThread()?.getStackTrace()!!
        if (trace.size > 6) {
            // Finding relevant stack frames
            val location = trace.getFrameAfter("runs.Tester", "expect") // ERROR HERE
            val function = trace.getFrameAfter("runs.TesterRunner", "forFunction") // AND HERE
            println("at ${function?.getClassName()}.${function?.getMethodName()}(line:${location?.getLineNumber()})")
        }
        if (message != null)
            println(message)
    }
    else if (!skipSuccessful)
        println("OK")
}

我不明白。它说

 Type mismatch: inferred type is kotlin.Array<java.lang.StackTraceElement> but
 kotlin.Array<java.lang.StackTraceElement?> was expected

我既无法推断前者是如何推断的,也无法预期后者。特别是我不明白两个这样的假设来自一个方法调用。

我通过删除有问题的行来“修复”它,但我相信有人可以启发我。

4

1 回答 1

4

问题是函数getFrameAfter是在 上定义的kotlin.Array<java.lang.StackTraceElement?>,但您试图在变量上调用它trace,它是类型kotlin.Array<java.lang.StackTraceElement>

我们已经修复了 Kotlin Web Demo 上的示例,因此您可以再次尝试。

于 2014-09-01T08:46:37.227 回答