Zac Sweers
12/17/2024, 5:47 AMbnorm
12/17/2024, 2:56 PMIrGenerationExtension
. If you set the arguments at the call site to be null
, the compiler should automatically convert that to a call to the synthetic function/constructor and calculate all the mask/marker information needed.
Call-site lowering for JVM: https://github.com/JetBrains/kotlin/blob/266fcba624f8e93102ee8aa3ee5f6a4942fc7510/[…]tbrains/kotlin/backend/jvm/lower/JvmDefaultParameterInjector.kt
Declaration-site lowering for JVM: https://github.com/JetBrains/kotlin/blob/266fcba624f8e93102ee8aa3ee5f6a4942fc7510/[…]ins/kotlin/backend/jvm/lower/JvmDefaultArgumentStubGenerator.ktZac Sweers
12/17/2024, 3:21 PMbnorm
12/17/2024, 4:43 PM@JsonClass(generateAdapter = true)
value class ValueClass(val i: Int = 0)
And the issues is the adapter should use the default value from the constructor if it is not found when parsing JSON?
class ValueClassJsonAdapter : JsonAdapter<ValueClass>() {
override fun fromJson(reader: JsonReader): ValueClass? {
val i = if (reader.hasNext()) reader.nextInt() else /* use default from constructor */
return ValueClass(i)
}
override fun toJson(writer: JsonWriter, value: ValueClass?) {
if (value != null) writer.value(value.i)
}
}
Zac Sweers
12/17/2024, 4:44 PMZac Sweers
12/17/2024, 4:44 PMbnorm
12/17/2024, 4:53 PMIrExpression
from the constructor: https://github.com/JetBrains/kotlin/blob/master/plugins/kotlinx-serialization/kotl[…]kotlinx/serialization/compiler/backend/ir/DefaultValuesUtils.kt.
In your case, are you generating the JsonAdaptor as a nested class or a top-level class?Zac Sweers
12/17/2024, 4:53 PMZac Sweers
12/17/2024, 4:54 PM