gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:59:05 31 4
gpt4 key购买 nike

以下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>

最佳答案

问题
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

关于kotlin - 文件可以在IntelliJ插件中编译正常,但不能使用kotlinc命令行编译器进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53783719/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com