Optional Parameters in Kotlin



Last week I gave a talk on Kotlin at the Austin Droids/GDG Meetup Hosted at Evernote. After the talk a question came up about mixing and matching Java with Kotlin. If you aren’t familiar Kotlin is 100% compatible with Java, allowing you to mix and match both in your project. In particular the question was

How are optional parameters treated when calling a Kotlin function from Java


In particular if you have the following code:

class Book{

   fun update(title: String, subtitle : String = "No Subtitle", abridged : Boolean = false){
      
   }
}

In kotlin you can simply make a call like the following:

book.update("Arbian Nights")

Or just specify a particular parameter that you want:

book.update("Arabian Nights", abridged = true)

Or even change the optional parameter order:

book.update("Arabian Nights", abridged = true, subtitle = "An Oxford Translation")

Going back to the question, since Java doesn’t have support for optional parameters how is this handled? Does Kotlin generate all the different variations making any combination safe? I can see how trying to support this on the Java side could lead to problems with allowing changeable order. For example if the function was defined as this:

class Book{

   fun update(title: String, subtitle : String = "No Subtitle", author : String = “Jim Baca”){
      
   }
}

And we wanted to call this from the Java side. How would the compiler know the difference between:

book.update("Arabian Nights", “No Subtitle”);

And this method call?

book.update("Arabian Nights", “Jim Baca”);

Given the above it’s not surprising that Kotlin doesn’t support optional parameters on the Java side. I was hoping for optional parameters would define the following functions:

update(String title)
update(String title, String subtitle)
update(String title, String subtitle, String author)

Update: Thanks Kirill Rakhman for pointing out that you can use @JvmOverloads annotation before your method definition you get this desired effect.

Of course this would create an odd situation since it doesn’t allow for parameter reordering which. Optional parameters in Kotlin become mandatory in Java and the parameter order is the exact same as it is defined in Kotlin, unless @JvmOverloads annotation is used. E.g. if we had this definition in Kotlin:

class Book{

   fun update(title: String, subtitle : String = "No Subtitle", author : String = “Jim Baca”){
      
   }
}

Then the correct way of calling the function would be:

book.update("Arabian Nights", “No Subtitle”, “Jim Baca”);

However if we had this:

class Book{
   
   @JvmOverloads fun update(title: String, subtitle : String = "No Subtitle", author : String = “Jim Baca”){
      
   }
}

Then we could call it with any of the following

book.update("Arabian Nights");
book.update("Arabian Nights", "No Subtitle");
book.update("Arabian Nights", "No Subtitle", "Jim Baca");

Comments 2

  1. You can use `@JvmOverloads` to generate overloads with all default parameters set. Then you call them from Java.

  2. Pingback: kotlin | Learn Android The Easy Way

Leave a Reply

Your email address will not be published. Required fields are marked *