简体   繁体   English

如何在IntelliJ IDEA中配置用于暂存文件的Kotlin版本?

[英]How can I configure the Kotlin version used for scratch files in IntelliJ IDEA?

I've recently installed IntelliJ IDEA in an attempt to learn Kotlin. 我最近安装了IntelliJ IDEA,以尝试学习Kotlin。 I've set up a Kotlin project and configured IDEA to use the latest stable release of the Kotlin plugin (1.2.71-release-IJ2018.2-1). 我已经设置了一个Kotlin项目并配置了IDEA以使用Kotlin插件的最新稳定版本(1.2.71-release-IJ2018.2-1)。

The following code works fine if executed in a project file (eg src/main.kt ): 如果在项目文件(例如src/main.kt )中执行,以下代码可以正常工作:

sealed class Port
class InputPort<in T> : Port()
class OutputPort<out T> : Port()

However, if I try to execute this in a scratch file instead, I get these errors: 但是,如果我尝试在暂存文件中执行此操作,则会收到以下错误:

scratch.kts:2:25: error: cannot access '<init>': it is private in 'Port'
class InputPort<in T> : Port()
                        ^
scratch.kts:2:25: error: this type is sealed, so it can be inherited by only its own nested classes or objects
class InputPort<in T> : Port()
                        ^
scratch.kts:3:27: error: cannot access '<init>': it is private in 'Port'
class OutputPort<out T> : Port()
                          ^
scratch.kts:3:27: error: this type is sealed, so it can be inherited by only its own nested classes or objects
class OutputPort<out T> : Port()
                          ^

From the Kotlin documentation , I can see that this is because this usage of sealed classes is only valid in Kotlin 1.1+. Kotlin文档中 ,我可以看到这是因为这种密封类的用法仅在Kotlin 1.1+中有效。 This implies that the project files are being executed with >= Kotlin 1.1, whereas scratch files are using < Kotlin 1.1. 这意味着正在使用> = Kotlin 1.1执行项目文件,而临时文件正在使用<Kotlin 1.1。

What I'm wondering is why Kotlin scratch files don't appear to use the same version of Kotlin as the main project files do, and how I can specify the Kotlin version to use for executing the scratch files. 我想知道的是为什么Kotlin暂存文件似乎不使用与主要项目文件相同的Kotlin版本,以及如何指定用于执行暂存文件的Kotlin版本。

Below is an example of a run/debug configuration I use for my scratch files: 以下是我用于我的暂存文件的运行/调试配置的示例:

When executing the scratch file, I see the following command appear in the scratch output pane (formatting altered for clarity): 执行暂存文件时,我看到以下命令出现在暂存输出窗格中(为清晰起见,更改了格式):

"C:\Program Files\Java\jdk-11\bin\java.exe" ^
    -javaagent:C:\Users\<me>\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\182.4505.22\lib\idea_rt.jar=53595:C:\Users\<me>\AppData\Local\JetBrains\Toolbox\apps\IDEA-C\ch-0\182.4505.22\bin ^
    -Dfile.encoding=windows-1252 ^
    -classpath C:\Users\<me>\.IdeaIC2018.2\config\plugins\Kotlin\kotlinc\lib\kotlin-compiler.jar;C:\Users\<me>\.IdeaIC2018.2\config\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Users\<me>\.IdeaIC2018.2\config\plugins\Kotlin\kotlinc\lib\kotlin-stdlib.jar;C:\Users\<me>\.IdeaIC2018.2\config\plugins\Kotlin\kotlinc\lib\kotlin-script-runtime.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler ^
    -kotlin-home C:\Users\<me>\.IdeaIC2018.2\config\plugins\Kotlin\kotlinc 
    -script C:/Users/<me>/.IdeaIC2018.2/config/scratches/scratch_2.kts

It isn't a problem with your configuration. 这与您的配置无关。 Currently sealed class can be inherited only by its own nested class in Kotlin scripts. 当前密封类只能由其自己的Kotlin脚本中的嵌套类继承。

There is an issue in YouTrack: https://youtrack.jetbrains.com/issue/KT-20180 YouTrack中存在一个问题: https ://youtrack.jetbrains.com/issue/KT-20180

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM