文件可以在IntelliJ插件中编译正常,但不能使用kotlinc命令行编译器进行编译

时间:2018-12-14 16:40:36

标签: kotlin

下面的Kotlin源文件可以使用IntelliJ IDEA 2018.3.1中的Kotlin 1.3.11插件编译并完美运行。但是,它无法在Kotlin命令行编译器中进行编译,给出以下消息。代码是否有问题,或者我是否缺少类路径依赖项?

C:\tmp>type Main.kt
import java.nio.file.Files
import java.nio.file.Paths

fun main() {
    Files.lines(Paths.get("t.txt"))
            .use { lines -> lines.forEach { println(it) } }
}

C:\tmp>kotlinc -version
info: kotlinc-jvm 1.3.11 (JRE 1.8.0_192-b12)

C:\tmp>kotlinc Main.kt
Main.kt:6:14: error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
@InlineOnly public inline fun <T : Closeable?, R> ???.use(block: (???) -> ???): ??? defined in kotlin.io
            .use { lines -> lines.forEach { println(it) } }
             ^
Main.kt:6:20: error: cannot infer a type for this parameter. Please specify it explicitly.
            .use { lines -> lines.forEach { println(it) } }
                   ^
Main.kt:6:35: error: cannot choose among the following candidates without completing type inference:
@HidesMembers public inline fun <T> Iterable<???>.forEach(action: (???) -> Unit): Unit defined in kotlin.collections
@HidesMembers public inline fun <K, V> Map<out ???, ???>.forEach(action: (Map.Entry<???, ???>) -> Unit): Unit defined in kotlin.collections
            .use { lines -> lines.forEach { println(it) } }
                                  ^
Main.kt:6:53: error: unresolved reference: it
            .use { lines -> lines.forEach { println(it) } }
                                                    ^

C:\tmp>

1 个答案:

答案 0 :(得分:0)

问题

use函数未在kotlin-stdlib中定义,而是在kotlin-std-jdk7中定义(请参见https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/use.html

解决方案

kotlinc -classpath kotlin-stdlib-jdk7-1.3.11.jar Main.kt