Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use @jvmoverloads with interface in Kotlin

Tags:

java

kotlin

My codebase is mixed java and kotlin code. I'd like to use @JvmOverloads on interface method with default arguments. Like that:

@JvmOverloads
fun getClientCompanyId(clientId: Long, date: DateTime = DateTime.now()): Long

I can't do this unfortunately and I get the message that:

JvmOverloads cannot be used on interface methods

However if I use it on overridden function then I get

Platform declaration clash: The following declarations have the same JVM signature(getClientCompanyId(JLorg/joda/time/DateTime;)J):

  • @JvmOverloads public open fun getClientCompanyId(clientId: Long, date: DateTime = ...): Long
  • @JvmOverloads public open fun getClientCompanyId(clientId: Long, date: DateTime = ...): Long

and just for the record: when I try to put default value in overridden method I get the message that:

An overriding function is not allowed to specify default values on its parameters

Is it a possible thing to do in kotlin? Thanks for all the answers.

like image 789
xklakoux Avatar asked Jan 18 '17 14:01

xklakoux


People also ask

What is @JvmOverloads in Kotlin?

The @JvmOverloads annotation is a convenience feature in Kotlin for interoperating with Java code, but there is one specific use case on Android where it shouldn't be used carelessly. Let's first look at what this annotation does, and then get to the specific issue.

CAN interface have implemented methods Kotlin?

Android Dependency Injection using Dagger with Kotlin In this chapter, we will learn about the interface in Kotlin. In Kotlin, the interface works exactly similar to Java 8, which means they can contain method implementation as well as abstract methods declaration.

What is XJVM default option?

with -Xjvm-default=enable , only default method in interface is generated for each @JvmDefault method. In this mode, annotating an existing method with @JvmDefault can break binary compatibility, because it will effectively remove the method from the DefaultImpls class.

What does the @JvmField annotation do when applied to a field in Kotlin?

@JvmField indicates weather the kotlin compiler should generate getters/setters for this property or not. If its set then it will not generate getters/setters for this property . You can omit it in this case .


2 Answers

I believe the best you can do is to define the overloads yourself. e.g.:

fun getClientCompanyId(clientId: Long, date: DateTime): Long
fun getClientCompanyId(clientId: Long) = getClientCompanyId(clientId, DateTime.now())
like image 177
mfulton26 Avatar answered Oct 07 '22 11:10

mfulton26


Spoilers ahead: This answer is not satisfying.

I ran into a similar issue while converting a Java class and interface into kotlin. The only way that my legacy Java code accepts @JvmOverloads generated code was to change my kotlin interface into an open class and change all fun() into open fun()

Works but its not what we really want.

like image 43
Alexander Knauf Avatar answered Oct 07 '22 10:10

Alexander Knauf