How to use @jvmoverloads with an interface in Kotlin

My codebase is a mix of java and kotlin code. I would like to use the @JvmOverloads method for an interface with default arguments. For instance:

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

I cannot do this, unfortunately, and I get a message that:

JvmOverloads cannot be used for interface methods

However, if I use it with an overridden function, I get

Collision with a platform declaration: 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 the default value in an overridden method, I get a message stating that:

Prohibiting function cannot specify default values ​​for its parameters

Can this be done in Kotlin? Thanks for all the answers.

+5
source share
1 answer

I believe that the best you can do is determine the overload yourself. eg:.

 fun getClientCompanyId(clientId: Long, date: DateTime): Long fun getClientCompanyId(clientId: Long) = getClientCompanyId(clientId, DateTime.now()) 
0
source

Source: https://habr.com/ru/post/1263002/


All Articles