underscore for generic type argument produces compile error in Scala?

189 Views Asked by At

I have

trait MyBase[T <: MyBase[T]] {
  def myid: String
}

So writing


case class MyBaseContainer(myBase: MyBase[_])

produces Type bound error for T in MyBase

type arguments [_$1] do not conform to trait MyBase's type parameter bounds [T <: Playground.MyBase[T]]

while writing it as

case class MyBaseContainer[T <: MyBase[_]](myBase: T)

compiles whiteout any errors.

Why is this happening? Aren’t these two signatures for MyBaseContainer semantically equivalent?

Is there a syntactical way for constraining the type parameter MyBase takes to the required bound, without passing T as a type parameter to MyBaseContainer?

1

There are 1 best solutions below

0
zmerr On BEST ANSWER

As mentioned by Dmytro Mitin in the comments, this would work:

import scala.language.existentials

case class MyBase[T <: MyBase[T]] (
   myid: String
)


case class MyBaseContainer(myBase: MyBase[T] forSome {type T <: MyBase[T]})