Kotlin: Kotlin-script (.kts) cannot use regular co

2020-06-03 00:19发布

In my library's codebase, I have this package function: fun sayHello() = println("Hello there!")
The function is defined in the package org.jire.pomade

I would like to use this function in a .kts file like so: sayHello()

Unfortunately I can't seem to get code apart from Kotlin's own stdlib to work in Kotlin-script files.

The entirety of my script:

import org.jire.pomade.sayHello

sayHello()

The result of running the script:

pomade.kts:1:12: error: unresolved reference: jire
import org.jire.pomade.sayHello
           ^
pomade.kts:3:1: error: unresolved reference: sayHello
sayHello()
^

Anybody know why this is happening? Thanks.

4条回答
家丑人穷心不美
2楼-- · 2020-06-03 00:32

I suggest to use holgerbrandl/kscript to manage dependencies of your script.

查看更多
小情绪 Triste *
3楼-- · 2020-06-03 00:47

There is experimental support for maven imports in Kotlin scripts since 1.3.

Take a look at https://blog.jetbrains.com/kotlin/2018/09/kotlin-1-3-rc-is-here-migrate-your-coroutines/#scripting:

@file:Repository("https://jcenter.bintray.com")
@file:DependsOn("org.jetbrains.kotlinx:kotlinx-html-jvm:0.6.11")

import kotlinx.html.*
import kotlinx.html.stream.*

print(createHTML().html {
    body {
        h1 { +"Hello, World!" }
    }
})

And here is the KEEP: https://github.com/Kotlin/KEEP/blob/master/proposals/scripting-support.md.

查看更多
走好不送
4楼-- · 2020-06-03 00:52

Alternative is using the Kotlin REPL instead.

查看更多
Root(大扎)
5楼-- · 2020-06-03 00:56

This is a bug in the Kotlin plugin: https://youtrack.jetbrains.com/issue/KT-11618

查看更多
登录 后发表回答