Understanding type error that prevents reflective property use


I have a data class and I thought I’d write an extension method toMap that uses reflection:

fun Thing.toMap(): MutableMap<String, Any?> {
  val kc = this::class
  val m = mutableMapOf<String, Any?>()
  for (p in kc.memberProperties) {
    m.put(p.name, p.get(this))
  return m

The compiler says:

Out-projected type ‘KProperty1<out EmailVerificationAttempt, Any?>’ prohibits the use of ‘public abstract fun get(receiver: T): R defined in kotlin.reflect.KProperty1’

I’m wondering…

  1. What does that mean?
  2. What cast can I use to force it to work?

the answer in this link

i.e. using .getter.call(this) instead of .get(this)

solved a similar problem for me


Thanks, it works.