https://kotlinlang.org logo
Title
d

Dalinar

01/10/2019, 4:05 PM
if I have a top-level
fun
how can I make a static import for it in Java?
s

Shawn

01/10/2019, 4:08 PM
I just tried it, it sorta just works
f

fred.deschenes

01/10/2019, 4:08 PM
if for example your function is called "foo" in a "Bar.kt" file you'll need to import BarKt.foo
s

Shawn

01/10/2019, 4:09 PM
Right, you will need to import the
Kt
-suffixed class
IDEA will show you the list of said classes as you write the import statement
d

Dalinar

01/10/2019, 4:10 PM
I've been trying
import static package.BarKt.foo
and I can't get it to work, I can call the fun using
BarKt.foo()
but I can't figure out how to reduce that to just
foo()
with a static import
is it not possible?
s

Shawn

01/10/2019, 4:11 PM
No, it definitely is possible, I literally just wrote a working example
d

Dalinar

01/10/2019, 4:11 PM
ah ok maybe i'm doing something wrong
ah the error says a non-static method can't be referenced from a static context
but.. it is a top level fun, how can it not be static?
s

Shawn

01/10/2019, 4:13 PM
Well, it’s not static in the traditional JVM sense, but that shouldn’t matter in this case
d

Dalinar

01/10/2019, 4:13 PM
do I have to make an object?
in the kt file
s

Shawn

01/10/2019, 4:14 PM
nope
d

Dalinar

01/10/2019, 4:14 PM
ah hah ok i figured it out
s

Shawn

01/10/2019, 4:15 PM
What was wrong?
d

Dalinar

01/10/2019, 4:19 PM
ok, no I need to work on it some more.. something is strange here.. when I click the method with the error it goes to the java api
toString()
method but mine is actually
toString(Exception)
if I qualify the call that solves everything but I have a lot of these in my code and similar funs that i converted to java, and the code will just look nicer on all those calls unqualified - they are helper funs/methods
s

Shawn

01/10/2019, 4:21 PM
sounds kinda like you have some kind of collision or shadowing issue
d

Dalinar

01/10/2019, 4:21 PM
but shouldn't happen with different signatures, right?
ok, question
s

Shawn

01/10/2019, 4:22 PM
I mean, it shouldn’t, but you also shouldn’t have this much issue with the static import
d

Dalinar

01/10/2019, 4:22 PM
on your sample code, if you click alt-enter on your
BarKt
of the `BarKt.foo()`call, do you get the option to import it? I get no options
ok this is weird, now it is actively editing away my static import when i make it, changing it just to
import package.BarKt
s

Shawn

01/10/2019, 4:25 PM
if I start with
import package.BarKt
and invoke
BarKt.foo()
, I do get an intention on the
BarKt
segment for an on-demand static import for
BarKt
similarly if I pull up intentions on the
foo
call specifically, I get the option to add a static import for the function call
d

Dalinar

01/10/2019, 4:26 PM
ok, you were right.. if I change the name to
toZString()
it works
but I have no idea why it's colliding with different signatures
thanks for the help
👍 1
guess I will call it
asString()
a

Alowaniak

01/11/2019, 8:43 AM
It might be an IntelliJ optimizing imports issue?
s

Shawn

01/11/2019, 5:58 PM
I think it’s a Java limitation - class member methods with the same name as statically-imported ones will always trump the latter, even if the signatures differ. You can reproduce with a Kotlin file like this:
// Foo.kt
package foobar

fun frobnicate(widget: Int) = Unit
And a Java file containing this:
// Bar.java
package foobar;

import static foobar.FooKt.frobnicate;

public class Bar {
  public void frobnicate() {  }
  public static void qux() {
    frobnicate(42);  // Non-static method 'frobnicate' cannot be referenced from a static context
  }
}