In my Kotlin project I have following situation:
abstract class BaseConverter<T> {
abstract fun serializeValue(output: ByteArray, value: T, offset: Int = 0): Int
}
object BooleanConverter: BaseConverter<Boolean>() {
override fun serializeValue(output: ByteArray, value: Boolean, offset: Int): Int {
output[0 + offset] = if(value) 1.toByte() else 0.toByte()
return 1
}
}
Now from my test cases I try to call BooleanConverter.serializeValue(array, value) which does not give me an IDE error message. However when I try to run the test I get following error:
Caused by: org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong code generated [...] AnalyzerException: Argument 3: expected R, but found I
When I change my call to BooleanConverter.serializeValue(array, value, 0) everything works fine. But this makes my default value unnecessary. I also can not add the default value when overrideing the method because of:
an overriding function is not allowed to specify default values for its parameters
So why can't I call the method only with two arguments and is there any way to solve this?
You can use default parameters only with variables of parent class type (in which method with default parameters is declared). Try as a workaround:
val c: BaseConverter<Boolean> = BooleanConverter
c.serializeValue(array, value)
or without creating an extra variable:
(BooleanConverter as BaseConverter<Boolean>).serializeValue(array, value)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With