scalagenericsnested-generics

Scala - Extend Generic Type Parameter


Sorry if this is a duplicate. Had trouble finding with some basic searches.

If I have

trait Container[T] { data: T } 

I'm trying to have a trait extend Container such that data is a Traversable[T].

Would the following do that, and what does it mean/how would you read it?

trait Extension[T] extends Container[Traversable[T]]

Solution

  • Yes, Extension[T] is a Container[Traversable[T]], which means it can only hold Traversable[T] as data.

    Note that you might want to define Extension[+T] instead of Extension[T] (and do this for Container as well). This will means that Extension[Cat] is a subclass of Extension[Animal].