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...
As of Kotlin 1.2 it is still not possible.
As a workaround, to statically "extend" Environment class I am currently using:
It is not an ideal solution but IntelliJ/Android Studio code completion helps with the usage:
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
This idea is very popular in the Kotlin community, so I bet it'll be in soon enough.
I think this is not possible. Documentation says the following:
The
Math
class is aJava
class, not aKotlin
one and does not have acompanion
object in it. You can add aclamp
method to theDouble
class instead.