I think I've found a bug in Kotlin's compiler, but...
# getting-started
j
I think I've found a bug in Kotlin's compiler, but wanted to double-check with you. Here's a reproducible example:
Copy code
class Wrapper<T>(val wrappedValue: T, parent: Wrapper<*>? = null)

fun <T : Any?, V> Wrapper<T>.wrapperOf(getter: KProperty1<T & Any, V>): Wrapper<V?> {
    return Wrapper(wrappedValue?.let { getter.get(it) }, this)
}

fun main() {
    // does not compile
    Wrapper("foo" as String?).wrapperOf(String::length)

    // but compiles with an intermediate variable
    val getter = String::length
    Wrapper("foo" as String?).wrapperOf(getter)
}
Link to the playground If I comment the eleventh line, it compiles, despite using nearly the same code just below but with an intermediate value. What do you think? Am I missing something?
It also works when writing:
Copy code
Wrapper("foo" as String?).wrapperOf(String::length as KProperty1<String, Int>)
j
Which do you think is correct behavior? Compiling or not?
This also works, where you separate it into a nullable and non-nullable variant. I think the issue is using
T & Any
.
j
@Jacob I expect all the code to compile
@Youssef Shoaib [MOD] Yes, it works with separate variants. But I'm not searching for an alternative, I just wonder if this is a bug. The more I dig, the more I think it is one.