6

I have a Kotlin project where I make use of a Java library dependency that defines an Interface with a String name() method declaration.

In Java I'm able to use this Interfaces within enum declarations where the String name() method is implicitly implemented by the enum.

public interface Aspect {
   int index();
   String name();
}

In Java this is possible:

public enum CollisionType implements Aspect {
    ONE, TWO, THREE;

    private final Aspect aspect;
    private CollisionType() {
        aspect = CONTACT_ASPECT_GROUP.createAspect(name());
    }
    @Override
    public int index() {
        return aspect.index();
    }
}

If I try this within a Kotlin enum class, I get an [CONFLICTING INHERITED JVM DECLARATIONS] error because of the conflicting name "name". I tried to use the @JvmName annotation to define a different name as it is suggested to do by this kind of problem but I'm not able to use it properly for this problem.

enum class CollisionType : Aspect {
    ONE, TWO, TREE;
    val aspect: Aspect = CONTACT_TYPE_ASPECT_GROUP.createAspect(name())

    override fun index(): Int = aspect.index()
    @JvmName("aspectName")
    override fun name(): String = name
}

gives an errors: "The @JvmName annotation is not applicable to this declaration"

Is there any possibility to implement/use a given Java Interface defining the String name() method within a enum class in Kotlin?

Thanks

1 Answer 1

1

As far as I can see now best option for you is following:

interface Aspect2: Aspect {
  fun myName() = name()
}
enum class CollisionType : Aspect2 {
  ………
}

And so on

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.