https://kotlinlang.org logo
Title
s

snrostov

01/04/2018, 12:19 PM
Why in this code type
GT
cannot be inferred?
class A<T>(val t: T)
class B<T, out U: A<T>>(val t: T, val at: U)

fun <T> B<T, *>.withCapturedU(): B<T, A<T>> = this

private fun <FT> f(x: B<FT, *>) {
  g(x) // ERROR, why?
  g(x.withCapturedU()) // OK
}

private fun <GT>  g(y: B<GT, A<GT>>) {
  println(y)
}
a

Andreas Sinz

01/04/2018, 3:00 PM
*
seems to put the compiler off. if I change it to
x: B<FT, A<FT>
it works fine
@snrostov ok, its not the
*
, but rather the variance of your generics, if you define
T
inside
A
and
B
as
out
, it works fine
s

snrostov

01/04/2018, 5:17 PM
@Andreas Sinz, yes, but why first substitution in compiller error message is not suitable? Moroover it is not highlighted as red. Looks like compiller (or at least diagnostic formatting) error. And why variant using `withCpaturedU`is working?
a

Andreas Sinz

01/04/2018, 6:14 PM
@snrostov because
*
can be
A<FT>
or any subclass of
A<FT>
together with
FT
being invariant means the compiler is not able to ensure that you'll always have
A<GT>
inside your
B<GT, *>
. not sure about the red highlighting
s

snrostov

01/05/2018, 10:33 AM
Ah, I see now. Thanks for explanation!