8
我在理解scala的类型范围系统时遇到了一些麻烦。我想要做的就是保存类型T的项目,可以遍历A型的项目我至今是一个holder类:Scala类型参数范围
class HasIterable[T <: Iterable[A], A](item:T){
def printAll = for(i<-item) println(i.toString)
}
val hello = new HasIterable("hello")
本身成功编译的类,但在尝试创建在hello
值给我这个错误:
<console>:11: error: inferred type arguments [java.lang.String,Nothing] do
not conform to class HasIterable's type parameter bounds [T <: Iterable[A],A]
val hello = new HasIterable("hello")
^
我本来期望hello
解决在这种情况下,一个HasIterable[String, Char]
。这个问题如何解决?
会你介意解释为什么这个工作(而另一个不)? – dhg
这对我有用,谢谢!是的,为什么<%在这种情况下工作? --aha我看到你的编辑。谢谢:) – Dylan
@pelotom:很好的解释。谢谢! – dhg