https://kotlinlang.org logo
Title
s

Stefan Beyer

06/06/2019, 11:52 AM
Does anyone know how to auto import an overloaded array access operator in intellij? Example:
// java
package com.example.legacy
public class Thing {
    public String get(String s) {
        return s;
    }
}
// kotlin
package com.example.extensions
import com.example.legacy.Thing
operator fun Thing.get(index: Int) = index
// kotlin
package com.example.business.logic
import com.example.legacy.Thing
fun doStuff(thing: Thing) {
    val x = thing[1] // compiler says "String expected, but Int found"
}
At
val x = thing[1]
there is a compile error, because the extension in
com.example.extensions
is not yet imported. But I cannot conveniently import it... When I instead write it as a method call (
val x = thing.get(1)
) it tells me that it can import it and I can just hit Alt+Enter and the import is added. Then I can change it back to the array access syntax. is there a shorter and more convenient way to get this import line added?
k

kralli

06/06/2019, 12:13 PM
But you're right, Intellij does not provide a quick import option for the short version.
s

Stefan Beyer

06/06/2019, 4:05 PM
yeah I can write the import myself, but... why should I ever want to manually write import statements? 😄