Xad Kile
05/04/2024, 7:46 AMC
is declared as below and performs some side effect on external variables. The external variables (q,__x,k,j
) are counted as parameter to the primary constructors, and I can't invoke such constructor with only the 3 constructor arguments.
I want to map .parameters
to dynamically create arguments to pass to this constructor, but in this case .parameter
does not contain the correct number of parameter (which is 7), but only 3.
This does not happen if I move the variables out of the main
function
Is this expected and how to deal with this?
fun main() {
var q = false
var __x = false
var k = false
var j = false
data class C(val i: Int, val str: String, val b: Boolean) {
init {
q = true
__x = true
k = true
j = false
}
}
println(C::class.primaryConstructor?.call(1, "", true)) // this throws exception IllegalArgumentException: Callable expects 7 arguments, but 3 were provided
println(C::class.primaryConstructor?.call(BooleanRef(),BooleanRef(),BooleanRef(),BooleanRef(),1, "", true)) // this works
println(C::class.primaryConstructor?.parameters?.size) // this prints 3, not 7
}
Sam
05/04/2024, 8:24 AMSam
05/04/2024, 8:26 AMXad Kile
05/04/2024, 8:02 PM