62

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

9
  • 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). Commented Nov 25, 2015 at 8:35
  • i think it is going to be possible in the future releases Commented Nov 25, 2015 at 11:59
  • 1
    Doesn't coerceIn (kotlinlang.org/api/latest/jvm/stdlib/kotlin/coerce-in.html) what you need? Commented Nov 26, 2015 at 7: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" Commented Nov 27, 2015 at 17:54
  • 1
    @Eric then only you can do is to declare top-level function (not extension) and put it to the specific package Commented Nov 30, 2015 at 18:45

4 Answers 4

54

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

Sign up to request clarification or add additional context in comments.

5 Comments

To whoever falls in this answer, please vote up the jetbrains post so we can have that feature earlier!
As of today the feature request remains open.
This idea is very popular in the Kotlin community, so I bet it'll be in soon enough . Aug 2022 Issue is still open.
@apouche YUP! Basically all movement was halted once Roman Elizarov took over. I'm still salty.
This is now a proposal for Kotlin 2.0 at github.com/Kotlin/KEEP/issues/348
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.

Comments

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()

Comments

2

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

Official announement: kotlin conf 2023

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.