Let's examine how functions with default argument values are compiled in Kotlin to see if there's a difference in method count. It may differ depending on the target platform, so we'll look into Kotlin for JVM first.
For the function fun foo(parameter: Any, option: Boolean = false)
the following two methods are generated:
- First is
foo(Ljava/lang/Object;Z)V
which is being called when all arguments are specified at a call site.
- Second is
synthetic bridge foo$default(Ljava/lang/Object;ZILjava/lang/Object;)V
. It has 2 additional parameters: Int
mask that specifies which parameters were actually passed and an Object
parameter which currently is not used, but reserved for allowing super-calls with default arguments in the future.
That bridge is called when some arguments are omitted at a call-site. The bridge analyzes the mask, provides default values for omitted arguments and then calls the first method now specifying all arguments.
When you place @JvmOverloads
annotation on a function, additional overloads are generated, one per each argument with default value. All these overloads delegate to foo$default
bridge. For the foo
function the following additional overload will be generated: foo(Ljava/lang/Object;)V
.
Thus, from the method count point of view, in a situation when a function has only one parameter with default value, it's no matter whether you use overloads or default values, you'll get two methods. But if there's more than one optional parameter, using default values instead of overloads will result in less methods generated.