Kotlin function default arguments from java

given following Kotlin class:

  class Foo {   public fun bar(i: Int = 0): Int = 2 * i   }

How should I call 'bar' function without any parameter from a java/groovy code?

  def f = new Foo()   f.bar() //throws:  java.lang.IllegalArgumentException: Parameter specified as non-null contains null

I highly suspect that when Groovy silently passes null whenever the user didn't supply enough arguments. The best way is to pass the value explicitly.

The way Kotlin implements default arguments is by having an extra function with an extra parameter, and this is not what you want to use from Java/Groovy.

For those curious for the solutions to this question, we can use @JvmOverloads annotation. As stated here the original author continue the discussion on StackOverflow instead.

2 Likes