https://kotlinlang.org logo
#getting-started
Title
# getting-started
h

hfhbd

03/02/2023, 7:53 PM
How do you get the String companion from Java? Non native classes works well, but I am not able to get mapped Kotlin classes 🤔
e

ephemient

03/02/2023, 7:58 PM
kotlin.jvm.internal.StringCompanionObject.INSTANCE
but why do you want to get it in Java?
h

hfhbd

03/02/2023, 8:00 PM
Thanks. Is it "safe" to use the
internal
package? :D I want to call some kotlin functions using the companion as a receiver like:
fun String.Companion.foo()
e

ephemient

03/02/2023, 8:21 PM
it's not a public API, but it can't change without breaking binary compatibility with existing Kotlin libraries, so… somewhat safe?
Kotlin functions defined like that aren't designed to be callable from Java though, I'd either change their definition or switch the caller to Kotlin
h

hfhbd

03/02/2023, 8:30 PM
That's true, and yeah, using Kotlin is always nicer. But creating another wrapper in Kotlin is annoying too, and now knowing the companion object its usage is okay.
2 Views