Kotlin 默认构造函数

Kotlin default constructor

docs 说:

On the JVM, if all of the parameters of the primary constructor have default values, the compiler will generate an additional parameterless constructor which will use the default values. This makes it easier to use Kotlin with libraries such as Jackson or JPA that create class instances through parameterless constructors.

但情况似乎并非如此:

Welcome to Kotlin version 1.2.71 (JRE 10.0.2+13-Ubuntu-1ubuntu0.18.04.2)
Type :help for help, :quit for quit
>>> class A(val x: Int = 1, val y: Int = 2)
>>> for (c in A::class.java.constructors) println(c)
public Line_0$A(int,int,int,kotlin.jvm.internal.DefaultConstructorMarker)
public Line_0$A(int,int)
>>> 

我错过了什么?

我认为 REPL 将 kotlin 代码作为脚本运行,无法完全编译。

当运行 test.kt :

class A(val x: Int = 1, val y: Int = 2)
fun main(args: Array<String>) {
    for (c in A::class.java.constructors) println(c)
}

kotlinc test.kt -include-runtime -d test.jar
kotlin test.jar

它正确打印

public A(int,int,int,kotlin.jvm.internal.DefaultConstructorMarker)
public A()
public A(int,int)

当运行 test.kts:

class A(val x: Int = 1, val y: Int = 2)
for (c in A::class.java.constructors) println(c)

kotlinc -script test.kts

它打印

public Test$A(int,int,int,kotlin.jvm.internal.DefaultConstructorMarker)
public Test$A(int,int)

与 REPL 相同。

所以可以肯定地说它编译 无参数构造函数。