Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

covariance - In scala, how could a covariant type parameter be an upper bound of an abstract type, but not another invariant type parameter?

The following code demonstrates it clearly:

trait Poly1Group[-IUB, +OUB] {

  trait Case[-I <: IUB] {

    type Out <: OUB // <------------------------------- success

    def apply(v: I): Out

  }

  trait ==>[
      -I <: IUB,
      O <: OUB //<------------------------------------- fail
  ] extends Case[I] {

    final type Out = O
  }

  def apply[I <: IUB](v: I)(implicit ev: Case[I]): ev.Out = ev(v)
}

It should be noted that both Out and O are invariant, so they should impose no constraint on the variance of OUB. Yet one succeed and one fails:

[Error] .../Poly1Group.scala:16: covariant type OUB occurs in contravariant position in type  <: OUB of type O
one error found

How could it be possible?

question from:https://stackoverflow.com/questions/65944627/in-scala-how-could-a-covariant-type-parameter-be-an-upper-bound-of-an-abstract

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...