0

I am trying to create an extension function on java.lang.System to refactor currentTimeMillis usage to the following System.currentTimeinSeconds() extension method that returns a String. (This is done to avoid duplicate code and to reuse it in the code)

This is what I have tried but its not working:

fun System.currentTimeInSeconds () : String {
    return ((this.currentTimeMillis / 1000).toString()) 

Any guidance as to why this is not working? Thanks.

u-ways
  • 6,136
  • 5
  • 31
  • 47
B.56
  • 11
  • 1
  • 1
    ... and whats your question? – Zabuzard Mar 31 '23 at 16:29
  • 1
    why return string ??? – mah454 Mar 31 '23 at 16:31
  • Because where it is being called requires a String – B.56 Mar 31 '23 at 16:32
  • For what do you need this method for? Feels like you are approaching this in a rather rusty Java legacy way instead of either considering modern Java or Kotlin options, revolving around `Instant` or `Duration` (Java/Kotlin variants) – Zabuzard Mar 31 '23 at 16:32
  • If you're trying to create a "static" extension function, then that's not really supported, especially if the receiver type is a Java class. There's also not much reason to _require_ an extension function in this case; a regular top-level function would work just fine. E.g., `fun currentTimeSeconds() = System.currentTimeMillis() / 1000` – Slaw Mar 31 '23 at 16:32
  • Downvoted because your question is unclear and generally rather low quality/effort. Please see [ask], thanks. – Zabuzard Mar 31 '23 at 16:34
  • @Slaw I already have this top level function and it works just fine, however am trying to still see if it is possible to do it as an extension function – B.56 Mar 31 '23 at 16:39
  • 1
    Then the answer is "no, this isn't possible". The closest you could get to this is to define the extension function on the companion object, but Java classes don't have companion objects. Also, "_Because where it is being called requires a String_", may be true, but I think this function should still return a `Long`. Or rename it to something like `currentTimeSecondsAsString()`. – Slaw Mar 31 '23 at 17:01
  • Please remove the java tag. Just because something runs on the JVM doesn't mean it's java. – Sören Mar 31 '23 at 17:18
  • I mean, what you should really be using is `Instant.now()`. – Louis Wasserman Mar 31 '23 at 22:06

3 Answers3

2

You cannot do this. Kotlin currently only supports extension functions on objects, not classes, and System is a class.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

Welcome to SO, first, it's important to reiterate the difference between a class and an object:

  • A class is a blueprint or a template for creating objects. It defines the properties and behaviour of objects.
  • On the other hand, an object is an instance of a class. It is created using the blueprint or template provided by the class.

Now that's out of the way, you can find decent amount of information of Kotlin Extension functions here:

The documentation states:

you can write new functions for a class or an interface from a third-party library that you can't modify. Such functions can be called in the usual way, as if they were methods of the original class. This mechanism is called an extension function. There are also extension properties that let you define new properties for existing classes.

And if you further looked down:

The this keyword inside an extension function corresponds to the receiver object (the one that is passed before the dot). Now, you can call such a function on any MutableList:

So the extension function acts on a received object, not the blueprint (class) itself.

System is a class and the currentTimeMillis() is a static native method. Therefore, when you try to create an extension function for it, your extension function will be called on the class itself, not on an object of the class in your case as you were calling the static method all along from System.

Next, does your requirements really need an extension function for this? You're calling a static, creating a function should be enough. (i.e. You have all the information you need)

fun stringifedTimeInSeconds() =
    System.currentTimeMillis().div(1000).toString()

Finally, it might be worth reading some answers in the following question:

Hope this helps.

u-ways
  • 6,136
  • 5
  • 31
  • 47
0

By the way, there is a proposal in the official Kotlin Evolution and Enhancement Process (KEEP) for this.
You can check out the details in Kotlin statics and static extensions.
But unfortunately we won't see any progress in the near feature, because the proposal was removed from the current roadmap.

Removed Item https://kotlinlang.org/docs/roadmap.html#new-items

Tobse
  • 1,176
  • 1
  • 9
  • 22