Primitive conversion from Kotlin to Java

MCMatan :

When trying to call a java method (constructor in this case) that accepts the type: java.lang.Double?

Example (AuthRequestModel is a Java modal):

AuthRequestModel(amount = 10.toDouble())

Resolves with error:

Type mismatch. Required: Java.lang.Double? Found: Kotlin.Double

What is the correct way of converting from Kotlin Double (or any other primitive) to Java?

For now, I added a conversion method that looks like so:

Imp:

fun Double.asJava(): java.lang.Double = this as java.lang.Double

Usage:

  AuthRequestModel(amount = 10.toDouble().asJava())

But this is force casting, and I would assume there is a native way of achieving the same result...

JB Nizet :

If AuthRequestModel was a Java class, you wouldn't be able to use named parameters to call its constructor. My guess is that AuthRequestModel is a Kotlin class which has been poorly translated to Kotlin. Fix the translation so that it uses a plain Double or Double?.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135843&siteId=1