0

I have the following field extension

import com.google.gson.annotations.SerializedName

val Enum<*>.serializedName: String get() = javaClass.getField(name).getAnnotation(SerializedName::class.java).value

Now if my enum has the same field name as serializedName extension, how could I access the extension field. My current approach is to cast to Enum

enum class MyEnum {
    FIELD_1,
    FIELD_2
    ;
    val serializedName = (this as Enum<*>).serializedName 
}
Quang Ngô
  • 45
  • 1
  • 5
  • 2
    Your solution is good enough. Ideally you shouldn't write extensions that conflict with existing field names, especially in your own project scope. – Pawel Mar 08 '20 at 13:04

1 Answers1

2

You can't. It described in the official kotline documentation https://kotlinlang.org/docs/reference/extensions.html

If a class has a member function, and an extension function is defined which has the same receiver type, the same name, and is applicable to given arguments, the member always wins. For example: