Home > Back-end >  How do I solve "Kotlin: Platform declaration clash:" on interface functions?
How do I solve "Kotlin: Platform declaration clash:" on interface functions?

Time:12-16

Compiling this interface declaration I have the error

Kotlin: Platform declaration clash: The following declarations have the same JVM signature (Function(Lkotlin/jvm/functions/Function1;)V): fun Function(action: (Int) -> Unit): Unit defined in IInterface fun Function(action: (String) -> Unit): Unit defined in IInterface

interface IInterface {
  fun Function(action: (Int) -> Unit)
  fun Function(action: (String) -> Unit)
}

But when I apply @JvmName attribute a have the error

Kotlin: '@JvmName' annotation is not applicable to this declaration

interface IInterface {
  @JvmName("Function1001")
  fun Function(action: (Int) -> Unit)
  fun Function(action: (String) -> Unit)
}

I've tried change interface to abstract class, but it doesn't help. Looks like @JvmName can be used only on class declaration.

What is the best practice to solve this problem?

CodePudding user response:

By default, @JvmName is disallowed in interfaces and open classes. The reason is explained in a comment to KT-20068:

@JvmName is problematic for overridable signatures, because then overriding in Kotlin and Java may start to disagree:

interface A {
  @get:JvmName("x")
  val foo: Foo
}

interface B {
  @get:JvmName("y")
  val foo: Foo
}

class C: A, B {
  // ???
  override val foo = ...
}

You can work around this limitation by adding

@Suppress("INAPPLICABLE_JVM_NAME")

but beware of the dangers.

  • Related