首页 > 解决方案 > 如何运行使用另一个 Kotlin 文件的 Kotlin 脚本?

问题描述

Kotlin 脚本kotlin-experiment/Play.kts

import experiment.*

println(sum(2, 2))

Kotlin 库kotlin-experiment/Play.kts

package experiment

fun sum(x: Int, y: Int) = x + y

编译运行命令:

kotlinc -script Play.kts

产生错误:

Play.kts:3:9: error: unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
...
println(sum(2, 2))
        ^

标签: kotlin

解决方案


目前,Kotlin 脚本不支持开箱即用的此功能。

通过使用 Kscript,您可以获得此功能和其他不错的功能 https://github.com/holgerbrandl/kscript


推荐阅读