0

I have been using Kotlin over Android for quite intensively. It does make programming fun again. Still, in some cases (mostly util classes where the name should be short and handy), when automatically converting Java to Kotlin, I would love to have an option to use @JvmStatic on static methods rather than converting callers to MyClass.Companion.Bar.

That is, in some specific cases, it would be nice to have

public static foo(Barian bar)

converted to

@JvmStatic
fun foo(bar:Barian)

so I can maintain the short calling syntax from Java:

MyClass.foo(bar)

rather than

MyClass.Companion.foo(bar)

Obviously, in most cases I agree it's bad manners for many reasons such as future compatibility, non-Kotlin spirit and many more, but in a few cases it can keep Java code (that uses my classes) shorter.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Maneki Neko
  • 1,177
  • 1
  • 14
  • 24
  • 2
    What is your question? – Mibac Sep 04 '17 at 07:44
  • On some central util classes, it is not nice that Java code has to call MyClass.Companion.foo(). Those are classes that were made to make the Java code "tidy" so I wanna keep them short. Unfortunately, the auto-conversion to Kotlin makes them under companion object. It is correct in most cases but there are cases that I want to make it add @JvmStatic to keep my Java code shorter. Or at least, take an existing companion object function and make its callers avoid Companion calls. (No biggie, can be done with regex later but if I can get it from the plugin - why not?) – Maneki Neko Sep 04 '17 at 14:57
  • I'm not sure if I get it right. You want auto conversion to Kotlin to mark Java `static` methods as `@JvmStatic`? As far as I know there's no such option – Mibac Sep 04 '17 at 14:59
  • Not for all. Just for specific cases. You know, classes where I have tens or hundred potential callers. Yeah, I imagined it is not possible. And I am absolutely with Jetbrains about that. Well, no biggie. Thanks – Maneki Neko Sep 04 '17 at 15:03
  • You could [file a feature request](https://youtrack.jetbrains.com/newIssue?project=KT&clearDraft=true&c=Type+Feature) if you really want to – Mibac Sep 04 '17 at 15:05

1 Answers1

0

You don't need to specify the Companion-namespace explicitly, when you decalre your "static" method like this:

class MyClass {

    companion object {
        fun foo() {}
    }
}

In this case you still can call it via:

MyClass.foo()

But nevertheless having static methods is not a Kotlin-idiomic way and should be avoided by using other features of this language.

Piwo
  • 1,347
  • 1
  • 12
  • 19
  • 1
    No. But a legacy Java code would have to use MyClass.Companion. Java is long and verbose enough. I want to keep legacy code "tidy". – Maneki Neko Sep 04 '17 at 14:50