kotlinkotlin-extension

How can one add static methods to Java classes in Kotlin


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


Solution

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

    Official announement: kotlin conf 2023