Guys, I have a very weird Java / Kotlin interoperability problem with the
Boolean
type. I’m using a Java API which defines two functions:
void setSomething(boolean enabled)
and
void setSomething(Boolean enabled)
, where
Boolean
is
java.lang.Boolean
here. The first function with the primitive Java type is marked deprecated. I’m trying to call the second function from Kotlin code but every time Kotlin tries to unbox the boolean into the primitive type, so it calls the deprecated function. How can I force Kotlin to use the boxed object type?
For example I tried
Copy code
api.setSomething(Boolean.valueOf(true))
but it’s still complaining.
v
Vampire
01/23/2023, 10:19 AM
Just a guess, but maybe
true as java.lang.Boolean
?
s
svenjacobs
01/23/2023, 10:20 AM
Unfortunately not, this gives a type error
Copy code
Required: kotlin.Boolean
Found: java.lang.Boolean
😞 1
svenjacobs
01/23/2023, 10:22 AM
and
true as Boolean
is just marked as an obsolete cast, still tries to call the deprecated function.
v
Vampire
01/23/2023, 10:22 AM
Maybe
true as Boolean?
?
👌 1
🎉 1
s
svenjacobs
01/23/2023, 10:23 AM
Yes, that’s it! Thank you!
svenjacobs
01/23/2023, 10:24 AM
It’s been so long since I last coded Java. I totally forgot that every object reference is nullable by default there 😅