Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overload resolution ambiguity when using Java enum in Kotlin

Tags:

kotlin

I am using the java enum io.confluent.kafka.schemaregistry.avro.AvroCompatibilityLevel in Kotlin code

public enum AvroCompatibilityLevel {
   NONE("NONE", AvroCompatibilityChecker.NO_OP_CHECKER),
   // ...

   public final String name;
   public final AvroCompatibilityChecker compatibilityChecker;

   // ...

In Kotlin I'd like to make use of

AvroCompatibilityLevel.NONE.name

but this leads to an

Overload resolution ambiguity. All these functions match:

public final val name: String? // coming from the field name

public final val name: String // coming from enum

Can anyone point out how to overcome this issue?

like image 638
marius_neo Avatar asked May 15 '26 10:05

marius_neo


1 Answers

You can write an extension property to extract the value of the member field "name" like this:

import kotlin.reflect.full.memberProperties

private val innerNameCache = mutableMapOf<AvroCompatibilityLevel, String>()
val AvroCompatibilityLevel.innerName: String
    get() = innerNameCache.getOrPut(this) {
        AvroCompatibilityLevel::class.memberProperties.filter { it.name == "name" }[0].get(this) as String
    }

fun main() {
    println(AvroCompatibilityLevel.NONE.innerName)
}
like image 140
Greyshack Avatar answered May 18 '26 14:05

Greyshack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!