简体   繁体   中英

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]]

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM