Compile time recursive type checking

I have this code within a companion object:

val x = Foo(::x.name)

There is a compilation error which can be easily fixed: (Type checking has run into a recursive problem.)

val x: Foo = Foo(::x.name)

My question is why does the compiler check for the type of x to get the name? Is it because it resolves ::x first before getting the name?

This solved issue about property name access says that it should be simply replaced with a string: https://youtrack.jetbrains.com/issue/KT-11531

I think it’s because ::x returns a KProperty interface, which has a lot more than just the name on it. It has the type of the property. The signature of KProperty is interface KProperty<out V> : KCallable<V>, but how does it know what V is if the property’s type isn’t specified?

1 Like