Kotlin - Generic Type Parameters Not Being Respected
问题 Consider the following example: import kotlin.reflect.KProperty1 infix fun <T, R> KProperty1<T, R>.test(value: R) = Unit data class Foo(val bar: Int) fun main() { Foo::bar test "Hello" } Given that test expects a value of type R , why in this context, where the property type is Int , does it allow me to pass a String ? 回答1: First, take a look at the declaration of the interface KProperty1 , which is: interface KProperty1<T, out R> : KProperty<R>, (T) -> R The important part here is the out