Short-hand property and function references

Property and function references currently look something like this

ClassWithLongName::propertyWithLongName

It would be useful to have a short-hand property/function reference where the receiver is already known. Take for example the following example, where the receiver is declared at the class level:

class Foo<T> {
    fun <P> getPropertyName(property: KProperty<T, P>): String {
        return property.name
    }
}

val foo = Foo<ClassWithLongName>()
val propertyName = foo.getPropertyName(ClassWithLongName::propertyWithLongName)

Note that whilst ClassWithLongName is declared by the Foo class’s generic type parameter, ClassWithLongName still has to be specified in order to obtain a reference to ::propertyWithLongName

It seems unnecessary to have to specify it twice, when T of KProperty<T, P> is already known.

Shorthand would allow syntax something like this:

val foo = Foo<ClassWithLongName>()
val propertyName = foo.getPropertyName(::propertyWithLongName)

Any thoughts as to whether this could become part of a future Kotlin language spec?

1 Like

You could use type aliases to help with long names:

class ThisClassHasALongName
typealias ShortName = ThisClassHasALongName

fun main() {
  println(ShortName::class)
}

You could, but that’s not really the point. It’s more of a workaround than a solution.

1 Like