Overload resolution ambiguity with public final field of a Java enum


#1

I have a Java enum similar to this one:

package com.example;

public enum FaultType {

    A("process"), B("electrical");

    public final String name;

    FaultType(String name) {
        this.name = name;
    }
}

The problematic thing is the public final field name.

If I access a field of this type from Kotlin

package com.example

class FaultUser(val faultType: FaultType) {
    fun show() = faultType.name
}

… IntelliJ shows the error:

Overload resultion ambiguity. All these functions match.

java-enum-kotlin

Tested with Kotlin 1.2.41 and IntelliJ 2018.1.2.

Is this a bug? Or do I have to do something special in Kotlin to access this field?


#2

To answer the question myself: It looks like name happens to be a very unfavorable name for an enum member! name collides with the name() method of the Enum class. It worked with Java because methods and fields are in different namespaces. My fix was to rename the enum field to displayName.

However, I wonder if Kotlin could support my original scenario. At least the error message is irritating.


#3

You might be able to using @JVMName annotations, but no guarantee: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#handling-signature-clashes-with-jvmname. Probably better to just do what you did and avoid the name clash.