问题标签 [kotlinc]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
456 浏览

gradle - 如何在本地 kotlinc 上运行远程调试

我想使用本地编译器编译一些示例 kotlin 项目。我从 githib 克隆 jetbrains/kotlin 项目并构建它。现在我在 /dist 文件夹中有本地编译器。我如何需要在示例项目中配置 gradle 以在其中使用此本地编译器并具有远程调试的能力?

0 投票
1 回答
111 浏览

java - 具有 const 参数的不可变对象是否会被优化为仅由 Kotlin 编译器实例化一次

Java 中有许多不可变类String和原始包装类,Kotlin 引入了许多其他类,如Range子类和不可变Collection子类。

对于迭代Ranges,来自控制流:if、when、for、while——我们已经知道的 Kotlin 编程语言:

范围或数组上的for循环被编译为不创建迭代器对象的基于索引的循环。

然而,在处理Ranges 的其他情况下,这种优化是不可能的。

当使用 const 参数创建此类不可变类时,或者更一般地说,使用 const 参数递归创建类时,只实例化一次类将带来性能提升。(换句话说,如果我们称其为 const 不可变实例化,则当且仅当它的所有参数都是常量或 const 不可变实例化时,实例化才是 const 不可变实例化。)因为 Java 编译器没有一种机制来知道是否一个类是不可变的,Kotlin 编译器是否根据其已知的不可变类的知识将此类类优化为仅实例化一次?

对于更具体的应用示例,请考虑以下代码:

第二个会带来任何性能改进吗?

0 投票
1 回答
117 浏览

kotlin - 如何使用 kotlin-native-linux 打开 kotlin repl?

kotlin-native-linux-1.3.61.tar.gz 我刚刚从这里下载了 kotlin 编译器: https ://github.com/JetBrains/kotlin/releases/tag/v1.3.61

但是,当我尝试按照文档中的建议打开 repl 时,运行:

我得到:

文档说:“我们可以在没有参数的情况下运行编译器以获得交互式外壳”

如何打开 kotlin repl?

0 投票
1 回答
150 浏览

kotlin - Kotlin 编译器:字节码中的`nop`

我正在检查kotlinc捕获 lambda 的字节码。并试图理解生成的字节码有nop指令的原因。

kotlinc -jvm-target 1.6 .

结果我得到

在主要功能中有几条nop指令。

如果我将用 编译相同的代码片段-Xno-optimizemain函数将如下所示

还有nops。

  1. nop在非优化代码中有 s 的原因是什么?(调试信息/...)
  2. 有什么理由nop在优化代码中有 s 吗?
0 投票
0 回答
122 浏览

java - 在命令行的 Kotlin Jar 文件中包含文件和目录

如何在命令行创建的 kotlin jar 文件中包含资源?用java我可以做到

在命令行的 Kotin 中,我们可以使用

创建一个 jar 文件,但我不能像使用 Java 一样将非 kotlin 文件添加到 jar 中。有谁知道这是否可以在没有 gradle/maven 的命令行上实现,我该怎么做?

我还希望能够在 jar 中包含 Kotlin 运行时

0 投票
1 回答
407 浏览

android - 在 kotlin REPL 和暂存文件中使用 json 类时出错

我正在开发一个需要处理从 Web API 检索到的 json 数据的 Android 项目。在项目中,我成功地使用了类JSONObjectJSONArray(包org.json)没有任何问题。

每次我想尝试一些 json 处理时总是运行编译、运行和导航应用程序很烦人,所以我决定尝试一下暂存文件。

我的暂存文件如下所示:

当我尝试运行该文件时,我收到一条错误消息,指出导入不成功:

据我了解,这是因为这些org.json类现在是 Android 的一部分,并且 Android 框架不能从草稿文件中获得。但是我该如何解决呢?我不应该能够以org.json某种方式从Android框架导入包吗?

0 投票
1 回答
82 浏览

kotlin - kotlin 控制台中定义的变量范围是什么?

kotlinc比如我通过命令进入kotlin控制台。

然后定义一个变量:val pi = 3.14,它的作用域是什么?

我试过const val PI = 3.14了,这将是一个错误,如下所示:

错误:const 'val' 只允许在顶层或对象中

所以,我想知道在控制台中输入的代码范围是什么?

0 投票
1 回答
172 浏览

kotlin - 运行 kotlinc 时抑制 Java 警告

每次我运行kotlinc时都会收到警告:

Java HotSpot(TM) 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

我该如何阻止这种情况发生?

0 投票
2 回答
289 浏览

kotlin - kotlin REPL 是否具有代码完成功能?

我刚刚打开kotlinc(显然默认为kotlin-jvm?)并输入了一些hello-world类型代码:

让我们创建一个实例:

希望repl将通过密钥为我们提供属性tab..?

每个都repl 发出哔哔声tab而不是提供该Person实例的属性。“以防万一”我明确地:quit打开kotlinc-jvm了 - 并得到了相同的行为。可以相信没有code completion可用的吗?我错过了一些开始的选择kotlinc吗?

0 投票
0 回答
316 浏览

kotlin - How can I import com.google.* using kotlinc, the command line kotlin compiler

Source code (standalone)

When I attempt to compile with kotlinc, it fails with "error: unresolved reference: google". Without the references to Gson and related constructs, the program compiles and the .jar file can be run without incident. But of course I can't get the JSON functionality.

When I tried to create a gradle project, I eventually got the build to succeed, but when I attempt to run the resulting .jar file, I get "no main manifest attribute" in that .jar file.

I did search for "no main manifest attribute, without success. In particular, this answer does NOT work for me.

I regret if there is an obvious answer, or if I failed to follow proper protocol. I have been working several hours on this and have tried several different approaches without success. I'd really like to run that program OUTSIDE Android Studio.

Thank you for your consideration.

Update: thanks to user who pointed out I needed to download a Gson .jar file.

Problem now is the kotlin compiler kotlinc does not appear to recognize the value of the environmental variable CLASSPATH. By using the -cp option, I can get kotlinc to locate that Gson jar, but with the resulting .jar file java cannot locate that Gson jar, either with -cp on the command line or CLASSPATH configured to locate the Gson jar.

Locating the Gson jar works with java. I found a simple Java program that references Gson. Both javac (the compiler) and java locate the same Gson jar through the CLASSPATH variable.

So why can't kotlinc create a jar such that the java runtime can locate the Gson jar? I'd really like to get this working outside of the Android Studio. From the command line it is much easier to complete initial verification and debugging. Notice my original program prints two different JSON strings. It's possible with Android Studio, but from my beginner perspective more difficult.

Thanks again for reading. Please forgive any violations of stackoverflow protocol.