可能重复: Scala: Abstract Types vs Generics 章“在Scala编程” 20.6“抽象类型”解释与在所得的以下代码结束使用以举例的抽象类型的: class Food
abstract class Animal {
type SuitableFood <: Food
def eat(food: SuitableFood)
}
class G
scala> class A { type T <: String; def f(a: T) = println("foo")}
defined class A
scala> (new A).f("bar")
<console>:9: error: type mismatch;
found : java.lang.String("bar")
required: _1.T where v
给出的方法foo下面的代码应该比较操作者明智给定参数bar与lowerBound和upperBound所有是相同的抽象类型Bar的。 trait Foo {
type Bar <: Ordered[Bar]
val lowerBound: Bar
val upperBound: Bar
def foo(bar: Bar) = bar >= lowerBoun
我想定义一个抽象类型的抽象递归数据结构。 事情是这样的: case class ParentA(name : String, children : List[ParentA]) extends Parent {
type PARENT = ParentA
}
case class ParentB(name : String, children : List[ParentB]) ex
我想使用抽象类型而不是类型参数。 在我的泛型类的构造函数,我想有泛型类型的参数,但代码不编译: class SomeOtherClass(val s: S){
type S
}
是“未发现:S型” Scala编译器错误 如果我使用一个类型参数,而不是一个抽象的类型,那么它的工作原理: class SomeClass[T](val t: T){
//...
}
是否斯