57

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...

Eric
  • 16,397
  • 8
  • 68
  • 76
  • 5
    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)`. – nhaarman Nov 25 '15 at 08:35
  • i think it is going to be possible in the future releases – voddan Nov 25 '15 at 11:59
  • 1
    Doesn't coerceIn (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/coerce-in.html) what you need? – Sergey Mashkov Nov 26 '15 at 07:28
  • 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" – Eric Nov 27 '15 at 17:54
  • @Eric 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. – Sergey Mashkov Nov 30 '15 at 18:43
  • 1
    @Eric then only you can do is to declare top-level function (not extension) and put it to the specific package – Sergey Mashkov Nov 30 '15 at 18:45
  • @voddan 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? – Jayson Minard Dec 25 '15 at 14:19
  • @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. – Jayson Minard Dec 25 '15 at 14:24
  • Possible duplicate of [Static extension methods in Kotlin](https://stackoverflow.com/questions/28210188/static-extension-methods-in-kotlin) – kyay10 Jun 12 '19 at 04:10

4 Answers4

51

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

Ky -
  • 30,724
  • 51
  • 192
  • 308
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.

Jayson Minard
  • 84,842
  • 38
  • 184
  • 227
aga
  • 27,954
  • 13
  • 86
  • 121
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()
1

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

Official announement: kotlin conf 2023

Hrudhay
  • 181
  • 3
  • 9