if I have a top-level `fun` how can I make a stati...
# announcements
d
if I have a top-level
fun
how can I make a static import for it in Java?
s
I just tried it, it sorta just works
f
if for example your function is called "foo" in a "Bar.kt" file you'll need to import BarKt.foo
s
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
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
No, it definitely is possible, I literally just wrote a working example
d
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
Well, it’s not static in the traditional JVM sense, but that shouldn’t matter in this case
d
do I have to make an object?
in the kt file
s
nope
d
ah hah ok i figured it out
s
What was wrong?
d
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
sounds kinda like you have some kind of collision or shadowing issue
d
but shouldn't happen with different signatures, right?
ok, question
s
I mean, it shouldn’t, but you also shouldn’t have this much issue with the static import
d
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
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
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
It might be an IntelliJ optimizing imports issue?
s
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:
Copy code
// Foo.kt
package foobar

fun frobnicate(widget: Int) = Unit
And a Java file containing this:
Copy code
// 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
  }
}