Hay una clase de biblioteca Java que tiene aVeryLongNameThatIDontWantToTypeEveryTime . Esta clase tiene algunos métodos estáticos con nombres genéricos: get() , abs() etc.
Ahora necesito construir llamadas complicadas con ellos en mi código kotlin como este:
aVeryLongNameThatIDontWantToTypeEveryTime.get(aVeryLongNameThatIDontWantToTypeEveryTime.abs(aVeryLongNameThatIDontWantToTypeEveryTime.get(...), aVeryLongNameThatIDontWantToTypeEveryTime.get(...)))Ahora, me gustaría usar una función de alcance local para no repetirme tan a menudo. Sin embargo, simplemente usando
with(aVeryLongNameThatIDontWantToTypeEveryTime) { get(abs(get(...), get(...))) }no funciona: se queja de que aVeryLongNameThatIDontWantToTypeEveryTime no tiene un objeto complementario. (Por supuesto que no, es una clase de Java).
La única "solución" es importar globalmente aVeryLongNameThatIDontWantToTypeEveryTime.* en el archivo, lo cual no es bueno ya que los nombres de los métodos son muy genéricos y podrían colisionar.
Puede usar el alias de importación para darle localmente un nombre más conveniente:
import com.example.aVeryLongNameThatIDontWantToTypeEveryTime as MyShortNameSi prefiere su solución con alcance, creo que la única forma en este momento es especificar su propio "alcance":
object aVeryLongNameThatIDontWantToTypeEveryTimeScope { inline fun get() = aVeryLongNameThatIDontWantToTypeEveryTime.get() inline fun set() = aVeryLongNameThatIDontWantToTypeEveryTime.set() inline fun abs() = aVeryLongNameThatIDontWantToTypeEveryTime.abs() } with (aVeryLongNameThatIDontWantToTypeEveryTimeScope) { get() set() abs() }Desafortunadamente, eso requiere reescribir todas las funciones, incluidos sus parámetros.
En el futuro, podría ser posible usar miembros estáticos de Java de manera similar a los objetos complementarios. Hay tareas similares en YouTrack de Kotlin.