<@U0E9C04JE> `var occupant: T` happens in invarian...
# announcements
r
@Dave Leeds
var occupant: T
happens in invariant position and you are stating that
crate: Crate<in Dog>
is contravariant. If you change
crate: Crate<in Dog>
to
crate: Crate<out Dog>
or just
crate: Crate<Dog>
it will compile. I'm guessing star projections are equivalent to existential types and unaffected by variance and
Animal
is all it can infer there based on your
T : Animal
constrain.
d
Thanks, @raulraja. I haven't done much with Scala, Haskell, etc, so I'm not familiar with existential types, but your description of star-projections sounds correct. My big question is: since star-projection uses the type parameter constraint as the most specific type that it can infer, why can't in-projections do the same?
star-projection uses
Animal
, but in-projection is stuck using
Any?
.
m
This does actually seem particularly odd to me, especially considering that:
Copy code
fun Dog.withInProjection(crate: Crate<in Dog>) {
    val starCrate: Crate<*> = crate
    val occupant: Animal = starCrate.occupant
}
does in fact compile.
FWIW, it seems to work in Java