How do I setup multiple type bounds in Scala?
GenericsScalaGenerics Problem Overview
I want to be able to declare something like this:
trait Narrowable[A] extends Iterable[A] {
def narrow[B <: A & B <: AnyRef] : Iterable[B]
}
That it, the type B
should be both a subtype of A
and AnyRef
. Is this possible?
Generics Solutions
Solution 1 - Generics
Use Compound Type:
trait Narrowable[A] extends Iterable[A] {
def narrow[B <: A with AnyRef] : Iterable[B]
}