https://kotlinlang.org logo
e

Edoardo Luppi

11/02/2020, 12:31 PM
If I need to pass the current JVM class to a static method but I don't really have a class, is this the only way to do it? See image.
k

kralli

11/02/2020, 12:36 PM
It there a specific reason for why you want your icons to be accessible on top-level? If not, maybe using an
object
could solve this problem and keep the top-level domain from being polluted.
e

Edoardo Luppi

11/02/2020, 12:38 PM
@kralli not really a reason tbh. It's that I started that way and... we are here 😅
k

kralli

11/02/2020, 12:39 PM
Copy code
object CCIcons {

    @JvmField val ICON = getIcon("/test.svg", CCIcons::class.java)
}
e

Edoardo Luppi

11/02/2020, 12:39 PM
@kralli and I cannot change it as I have external dependencies on those.
@kralli yep! That would be the correct refactoring, but maybe there is a way to avoid it
k

kralli

11/02/2020, 12:40 PM
So you’re using the top-level variables from Kotlin in a different module?
Or only from Java?
e

Edoardo Luppi

11/02/2020, 12:42 PM
@kralli to be specific, that code comes from an IntelliJ plugin. And I have other plugins depending on those top level definitions (both Java and Kotlin)
@kralli maybe I'm lucky. Since I have named that JVM file
CCIcons
I can refactor as you showed without losing binary compatibility.
k

kralli

11/02/2020, 12:47 PM
That’s why I brought it up, though this does only apply to binary compatibility. In Kotlin it will obviously make a difference.
so no source code compatibility in Kotlin
e

Edoardo Luppi

11/02/2020, 12:50 PM
@kralli that's fine. I'll give this a try! Thanks!
2 Views