我如何在Scala中设置多个类型边界? [英] How do I setup multiple type bounds in Scala?
本文介绍了我如何在Scala中设置多个类型边界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
trait Narrowable [A] extends Iterable [A] {
def narrow [B<:A& B<:AnyRef]:Iterable [B]
}
它的类型 B
应该既是 A
和 <$ c $的子类型C> AnyRef 。
解决方案
使用化合物类型:
> trait Narrowable [A]扩展Iterable [A] {
def narrow [B with AnyRef B:A]:Iterable [B]
}
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?
解决方案
Use Compound Type:
trait Narrowable[A] extends Iterable[A] {
def narrow[B <: A with AnyRef] : Iterable[B]
}
这篇关于我如何在Scala中设置多个类型边界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文