2

There's have a Java library's class which has aVeryLongNameThatIDontWantToTypeEveryTime. This class has a few static methods with generic names: get(), abs() etc.

Now I need to construct complicated calls with them in my kotlin code like this one:

aVeryLongNameThatIDontWantToTypeEveryTime.get(aVeryLongNameThatIDontWantToTypeEveryTime.abs(aVeryLongNameThatIDontWantToTypeEveryTime.get(...), aVeryLongNameThatIDontWantToTypeEveryTime.get(...)))

Now, I would like to use a local scoping function in order to not repeat myself so often. However, simply using

with(aVeryLongNameThatIDontWantToTypeEveryTime) {
  get(abs(get(...), get(...)))
}

does not work: It complains that aVeryLongNameThatIDontWantToTypeEveryTime does not have a companion object. (Of course it doesn't, it's a Java class.)

The only "solution" is to globally import aVeryLongNameThatIDontWantToTypeEveryTime.* in the file which isn't great since the method names are so generic and could collide.

Atemu
  • 295
  • 3
  • 12
  • Consider using [Type aliases](https://kotlinlang.org/docs/type-aliases.html)? – Ricky Mo Oct 22 '21 at 11:05
  • Good way to cut down on the length for sure but you still need to have `someAlias.` everywhere. That's fighting a symptom (long name) rather than the actual problem (repetition). – Atemu Oct 22 '21 at 11:15

1 Answers1

3

You can use import alias to give it locally a more convenient name:

import com.example.aVeryLongNameThatIDontWantToTypeEveryTime as MyShortName

If you prefer your scoped solution then I think the only way right now is to specify your own "scope":

object aVeryLongNameThatIDontWantToTypeEveryTimeScope {
    inline fun get() = aVeryLongNameThatIDontWantToTypeEveryTime.get()
    inline fun set() = aVeryLongNameThatIDontWantToTypeEveryTime.set()
    inline fun abs() = aVeryLongNameThatIDontWantToTypeEveryTime.abs()
}

with (aVeryLongNameThatIDontWantToTypeEveryTimeScope) {
    get()
    set()
    abs()
}

Unfortunately, that requires rewriting all functions, including their parameters.

In the future it could be possible to use Java static members similarly to companion objects. There are similar tasks in Kotlin's YouTrack.

broot
  • 21,588
  • 3
  • 30
  • 35
  • As mentioned above, the problem I want to resolve is repetition, not the amount I need to type/let the IDE autocomplete. Aliases still remove a good bit of visual noise though. – Atemu Oct 22 '21 at 11:19
  • Interesting solution but unfortunately it's not that simple in practice: The library has *tons* of methods and overloaded variants. Even just listing the ones I really need would result in a substantial amount of boilerplate, especially w.r.t. parameters. Only having access to a subset of the available methods is also not a great dev experience for autocomplete etc. Is there a more generic method of converting a Java Object with static methods into something `with()` can handle perhaps? – Atemu Oct 22 '21 at 11:33
  • 1
    I believe not, but maybe I miss something. I definitely don't like how some features of Kotlin are dependent on the existence of companions. For example, we can't add "static" extensions to a class if it doesn't have a companion. I believe someday Kotlin team has to unify companions and Java static members. There are quite a lot of issues like these in their tracker, e.g.: https://youtrack.jetbrains.com/issue/KT-11968 and all related. There is even suggestion: "This could be implemented by adding default companion objects to every class" - that would make your above code possible. – broot Oct 22 '21 at 11:45