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

Stefan Oltmann

03/28/2024, 3:14 PM
Extension functions apparently can't be JvmStatic... What can I do to improve the API for Java users here? https://github.com/Ashampoo/kim/blob/49b7f3abdbc7d5ab58e09255282a80e7e983285b/src/jvmMain/kotlin/com/ashampoo/kim/JvmKimExtensions.kt#L33 Without that available it's not very nice to use: https://github.com/Ashampoo/kim/blob/examples/examples/kim-java-sample/src/main/java/Main.java
JvmKimExtensionsKt
itself is available, but this looks strange and I don't want to showcase that. What are my alternative options to make a
Kim.readMetadata(file)
work in Java?
e

ephemient

03/28/2024, 3:15 PM
@file:JvmName
which is an ABI-incompatible change of course
s

Stefan Oltmann

03/28/2024, 3:16 PM
Thanks, will try that. As long as Kim is not a 1.x release I think I'm free to break sometimes 😄
Oh, that's an
JvmKimExtensions.kt:27:5 Unresolved reference: Kim
Because there is already a
Kim.kt
at https://github.com/Ashampoo/kim/blob/main/src/commonMain/kotlin/com/ashampoo/kim/Kim.kt
That Kim object is the central API to use the lib. in commonMain is takes parameters valid for all platforms and in specific source set it extents
In Kotlin code im import the extension function and it looks like it should. In Java code it's not so easy
Maybe I need to extract the common logic elsewhere and provide each platform with their very own Kim object, but I hope that there is an easier solution
h

hfhbd

03/28/2024, 3:49 PM
Never used it but you could try JvmMultifileClass: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-multifile-class/ To merge it with the existing Kim file
e

ephemient

03/28/2024, 3:50 PM
only works if everything is also a top-level file and not an
object
h

hfhbd

03/28/2024, 3:50 PM
But you already have an object, so why not add the function to Kim?
s

Stefan Oltmann

03/28/2024, 3:50 PM
Ok, understood. I think I create platform-specific Kim objects. Thank you 🙂
e

ephemient

03/28/2024, 3:51 PM
I don't think you want to do that. that will make it difficult to use Kim in common sourcesets
h

hfhbd

03/28/2024, 3:51 PM
You can also add a JVM only function by hiding it from Kotlin
s

Stefan Oltmann

03/28/2024, 3:51 PM
How?
h

hfhbd

03/28/2024, 3:52 PM
Deprecated.Hidden
s

Stefan Oltmann

03/28/2024, 3:52 PM
I look that up.
I don't think you want to do that. that will make it difficult to use Kim in common sourcesets
Good point.
@Deprecated(HIDDEN)
feels a bit wrong/hacky. It seems to be intended for libraries that are in the process of Kotlin migration.
2 Views