class Contravariant[-T](val other:T)
error: contravariant type T occurs in covariant position in type T of value other
However this one succeeds
class MMX[-T](x:T)
What is the difference?
Thanks
class Contravariant[-T](val other:T)
error: contravariant type T occurs in covariant position in type T of value other
However this one succeeds
class MMX[-T](x:T)
What is the difference?
Thanks
Copyright © 2021 Jogjafile Inc.
As Luis Miguel Mejía Suárez said, in the first example,
otheris a field, and fields cannot be contravariant (as Dmytro Mitin pointed out, not fields with modifierprivate[this]orprotected[this]), although they can be covariant. Consider this example, assuming your first example worked:Here you can see what happens (I used
@uncheckedVarianceto make it work).In the second example,
xis simply a parameter to your constructor, and parameters can be contravariant, so it works.