Why in this code type `GT` cannot be inferred? ```...
# announcements
s
Why in this code type
GT
cannot be inferred?
Copy code
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
*
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
@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
@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
Ah, I see now. Thanks for explanation!