Why is parameter in contravariant position?

前端 未结 2 765
梦毁少年i
梦毁少年i 2020-12-01 01:06

I\'m trying to use a covariant type parameter inside a trait to construct a case-class like so:

trait MyTrait[+T] {
  private case class MyClass(c: T)
}
         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-01 01:43

    Almost there. Here:

    scala> trait MyTrait[+T] {
         |   private case class MyClass[U >: T](c: U)
         | }
    defined trait MyTrait
    

    Which means MyClass[Any] is valid for all T. That is at the root of why one cannot use T in that position, but demonstrating it requires more code than I'm in the mood for at the moment. :-)

提交回复
热议问题