Tem*_*Dev 1 kotlin kotlin-coroutines
使用kotlinc-jvm 1.3.61和kotlinx-coroutines-core-1.3.3,以下代码无法编译。
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {}
}
Run Code Online (Sandbox Code Playgroud)
有错误
Error: Main method not found in class SomeExampleKt, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Run Code Online (Sandbox Code Playgroud)
但是,以下代码可以成功编译并运行。
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {}
print("") // The only addition
}
Run Code Online (Sandbox Code Playgroud)
谁能解释为什么只添加一条print语句就可以编译?
IR4*_*R42 10
main函数不应返回任何内容 ( Unit)。runBlocking返回其最后一个语句值并launch返回Job, 但是print一个Unit函数。指定返回值类型可能会解决这个问题。
import kotlinx.coroutines.*
fun main() = runBlocking<Unit> {
launch {}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
738 次 |
| 最近记录: |