How can one add static methods to Java classes in Kotlin
Asked Answered
A

4

60

Is it possible to add a new static method to the java.lang.Math class in Kotlin? Usually, such things are possible in Kotlin thanks to Kotlin Extensions.

I already tried doing the following in a file I made called Extensions.kt:

fun Math.Companion.clamp(value:Double,minValue:Double,maxValue:Double):Double
{
    return Math.max(Math.min(value,maxValue),minValue)
}

but Math.Companion could not be resolved...

Anne answered 25/11, 2015 at 8:18 Comment(9)
Why not add the function to the Double class? fun Double.clamp(min: Double, max Double), to be called like 1.0.clamp(2.0, 3.0).Marbut
i think it is going to be possible in the future releasesOvernight
Doesn't coerceIn (kotlinlang.org/api/latest/jvm/stdlib/kotlin/coerce-in.html) what you need?Mucor
Yes! Thanks @SergeyMashkov, that is what I needed in this case. However, I should rephrase my question as: "How can one add static methods to Java classes in Kotlin"Anne
@Anne Nobody can in fact. Kotlin's top-level extension functions are static functions of a facade class (generally it is $filename$Kt.class) and a receiver is just a first parameter. You can't have Math instance so you can't pass anyway. It just doesn't work like that. So general answer is: you can't and you shouldn't.Mucor
@Anne then only you can do is to declare top-level function (not extension) and put it to the specific packageMucor
@Overnight it hasn't been talked about that I'm aware, please link to a Kotlin slack chat or forum discussion, or even YouTrack ticket for that feature?Clang
@SergeyMashkov "Shouldn't" is speaking from opinion, it is obviously something that COULD be implemented in Kotlin using a different form of the function that either takes the Java/Kotlin class as the receiver or is receiver-less. Currently, there is no way to do this without a Companion object being declared. But nothing prevents such a feature in the future as compiler syntactic sugar.Clang
Possible duplicate of Static extension methods in KotlinBegga
E
2

Update on top of other answers: Static extensions will be available right after 2.0

Official announement: kotlin conf 2023

Embattle answered 9/5, 2023 at 14:0 Comment(0)
V
53

As of Kotlin 1.3, this is not possible. However, it's being considered for a future release!

To help this feature get implemented, go vote on this issue: https://youtrack.jetbrains.com/issue/KT-11968

Because all proposals are basically in limbo right now, I wouldn't hold my breath that this will get in any time soon

Vizierate answered 8/3, 2017 at 14:43 Comment(5)
To whoever falls in this answer, please vote up the jetbrains post so we can have that feature earlier!Rapp
As of today the feature request remains open.Alcoholometer
This idea is very popular in the Kotlin community, so I bet it'll be in soon enough . Aug 2022 Issue is still open.Bucket
@Bucket YUP! Basically all movement was halted once Roman Elizarov took over. I'm still salty.Vizierate
This is now a proposal for Kotlin 2.0 at github.com/Kotlin/KEEP/issues/348Thermocline
B
15

I think this is not possible. Documentation says the following:

If a class has a companion object defined, you can also define extension functions and properties for the companion object.

The Math class is a Java class, not a Kotlin one and does not have a companion object in it. You can add a clamp method to the Double class instead.

Brittain answered 25/11, 2015 at 8:37 Comment(0)
I
3

As of Kotlin 1.2 it is still not possible.

As a workaround, to statically "extend" Environment class I am currently using:

Class EnvironmentExtensions {
    companion object {
        @JvmStatic
        fun getSomething(): File {
            ...
            return Environment.something()
        }
    }
}

It is not an ideal solution but IntelliJ/Android Studio code completion helps with the usage:

val something = EnvironmentExtensions.getSomething()
Intellectuality answered 12/3, 2018 at 9:19 Comment(0)
E
2

Update on top of other answers: Static extensions will be available right after 2.0

Official announement: kotlin conf 2023

Embattle answered 9/5, 2023 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.