via reflection: how to call the constructor of a c...
# language-evolution
d
via reflection: how to call the constructor of a contextual class via
abstract fun callBy(args: Map<KParameter, Any?>): R
(as I want to have constructor args that have default values, to take their default values on instantiation)
Copy code
context(SomeCtxClass)
class ContextualClass(val some: String, val withDefault: String = "Default")
Copy code
val primaryConstructor: KFunction<ContextualClass> = ContextualClass::class.primaryConstructor!!
gives me a parameter list of size 2 (the direct class constructor parameters) but as the class has
context(SomeCtxClass)
I have to pass three!! args to the
paramMap
of
primaryConstructor.callBy(paramMap)
now I have an instance of the ctx object ... but how to "put" this into the paramMap which is a
Map<KParameter, Any?>)
(but I don't have a KParameter for that context object, as the
..::class.primaryConstructor
doesn't have a KParam for the context object
e
I think reflection is not supported yet for context receivers. It will be eventually when the feature is stable in 2.1 or so. You should open an issue.