Zach Klippenstein (he/him) [MOD]
04/08/2020, 4:55 PMdata class FunctionHolder<out T : Any>(
val f: (@UnsafeVariance T) -> Unit
)
fun caller(holder: FunctionHolder<*>) {
val f: (Any) -> Unit = holder.f
}
Zach Klippenstein (he/him) [MOD]
04/08/2020, 4:57 PMT
is specified as covariant, I would expect FunctionHolder<*>
to be equivalent to FunctionHolder<Any>
, and FunctionHolder<Any>.f
to be of type (Any) -> Unit
. And this is indeed the case in 1.3.
In 1.4-M1 (with IDE plugin 1.4-M1), it is also what the IDE thinks when doing code completion:Zach Klippenstein (he/him) [MOD]
04/08/2020, 4:59 PMholder.f
is apparently (Nothing) -> Unit
, which I would only expect if T
was contravariant.Zach Klippenstein (he/him) [MOD]
04/08/2020, 4:59 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 5:00 PMturansky
04/08/2020, 5:02 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 5:03 PMturansky
04/08/2020, 5:07 PMuseIR
option in kotlinOptions
?Zach Klippenstein (he/him) [MOD]
04/08/2020, 5:09 PMturansky
04/08/2020, 5:19 PMturansky
04/08/2020, 5:20 PMturansky
04/08/2020, 5:21 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 5:24 PMturansky
04/08/2020, 5:26 PMdata class FunctionHolder<out T : Any>(
val f: (@UnsafeVariance T) -> Unit
)
fun <T: Any> caller(holder: FunctionHolder<T>) {
val f: (T) -> Unit = holder.f
}
Type parameter for caller
?Zach Klippenstein (he/him) [MOD]
04/08/2020, 5:28 PMturansky
04/08/2020, 5:34 PMholder: FunctionHolder<*>
- not compiled
holder: FunctionHolder<Any>
- compiled
holder: FunctionHolder<T>
- compiledZach Klippenstein (he/him) [MOD]
04/08/2020, 5:50 PMturansky
04/08/2020, 5:58 PMZach Klippenstein (he/him) [MOD]
04/08/2020, 6:02 PMFunctionHolder<out Any>
, which is what the star-projection actually means.Zach Klippenstein (he/him) [MOD]
04/08/2020, 6:05 PMFunctionHolder
, not just properties with function types.turansky
04/08/2020, 6:11 PMholder: FunctionHolder<*>
it means that it can be FunctionHolder<String>
It that case error in val f: (Any) -> Unit = holder.f //holder.f: (String) -> Unit
is validZach Klippenstein (he/him) [MOD]
04/08/2020, 6:21 PMf
is not actually a String
, but that error is on the definition of FunctionHolder
itself and it is suppressed by @UnsafeVariance
, both in 1.3 and 1.4.Zach Klippenstein (he/him) [MOD]
04/08/2020, 6:24 PMout
on the parameter to caller
is the difference between error and no error.Zach Klippenstein (he/him) [MOD]
04/08/2020, 6:32 PM