kotlin - 如何运行使用另一个 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 脚本不支持开箱即用的此功能。
通过使用 Kscript,您可以获得此功能和其他不错的功能 https://github.com/holgerbrandl/kscript
推荐阅读
- python - 如何从整数的第 n 个元素(collatz 序列)开始显示答案?
- reactjs - 谷歌浏览器的formik输入自动填充
- google-chrome - 如何开启闪光灯?
- xaml - Xamarin.Forms XAML RelativeLayout:定义一个高度,其余部分动态填充
- qt - 如何向 TreeView 添加值?
- javascript - React Native 如何获取设备的方向
- javascript - 使用 jQuery 兄弟功能检查至少一个复选框
- typescript - 使用接口的键索引对象,而值是具有该键的返回类型的函数
- intellij-idea - 在 Intellij 中,来自 2 个不同项目(在不同目录中创建)的类文件相互干扰
- python - 从 on_raw_reaction_add discord.py 中检索消息