shaby
shaby

Reputation: 1381

Kotlin java interoperability is possible with default parameters but not with named parameters, why so?

Kotlin java interoperability is possible with default parameters using annotation, i.e

@JvmOverloads

Example, I can call this Kotlin function from Java class.

@JvmOverloads
int findVolume(length:Int, breadth:Int, height:Int = 20): Int {
retrun length*breadth*height
}

//Calling from Java class.

KotlingFileName.findVolume(5, 10)// No problem here, possible, third param is optional

But the same is not possible with named parameters.

@JvmOverloads
int findVolume(length:Int, breadth:Int, height:Int = 20): Int 
{
 retrun length*breadth*height
}

//Calling from Java

KotlingFileName.findVolume(length=5,breadth= 10, height=20) // Not possible, why?

Upvotes: 1

Views: 235

Answers (1)

duffymo
duffymo

Reputation: 308753

Java doesn't support named parameters - that's the easy one.

The default parameters are accommodated by generated byte code. The Kotlin compiler generates multiple versions of the method with default parameters. Your example calls the three parameter version of the Java method with the 3rd parameter assigned to the default parameter.

Upvotes: 2

Related Questions