How do I setup multiple type bounds in Scala?

GenericsScala

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionoxbow_lakesView Question on Stackoverflow
Solution 1 - GenericsWalter ChangView Answer on Stackoverflow