oxbow_lakes
oxbow_lakes

Reputation: 134320

How do I setup multiple type bounds in Scala?

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?

Upvotes: 79

Views: 18710

Answers (1)

Walter Chang
Walter Chang

Reputation: 11606

Use Compound Type:

trait Narrowable[A] extends Iterable[A] {
  def narrow[B <: A with AnyRef] : Iterable[B]
}

Upvotes: 121

Related Questions