Skip to content
Advertisement

Overload resolution ambiguity in third party java library called from kotlin

I wrote a small program in Kotlin which uses library https://github.com/KaptainWutax/SeedUtils, specifically the Dimension enum https://github.com/KaptainWutax/SeedUtils/blob/master/src/main/java/kaptainwutax/seedutils/mc/Dimension.java

When I call e.g. Dimension.OVERWORLD.name, I get Overload resolution ambiguity. I know what is the issue, the problem is that enum Dimension has name field, and the enum class itself has name field https://kotlinlang.org/docs/reference/enum-classes.html

The question is, what can I do about it. My current approach is to fork this library and all other 4 libraries I use and depend on it, renaming name in that enum to something else, but I hate having to fork 5 repos to rename single field.

Is there any other way to get around this? Can I specify somehow which name should be use? Is there a way to remove this ambiguity somehow by telling the JVM what to do?

Or is there nothing to be done and naming enum field name is effective way to make it unusable by Kotlin?

Advertisement

Answer

One workaround would be to write a helper method in Java, where it’s unambiguous:

public class DimensionHelper {
    public static String getName(Dimension dimension) {
        return dimension.name;
    }
}

Then from Kotlin, you could call DimensionHelper.getName() whenever you wanted to access the name of a Dimension. You can then add an extension method in Kotlin:

fun Dimension.getName() = DimensionHelper.getName(this);

… which will allow you to just use Dimension.OVERWORLD.getName().

It’s far from ideal, but it does avoid having to fork.

(It’s entirely possible that there’s a Kotlin-specific way of doing this that I’m unaware of, of course.)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement