例如,这个:
val queue = new BasicIntQueue with Doubling with Incrementing with Filtering
queue.put(1)
println(queue.get())
Run Code Online (Sandbox Code Playgroud)
将打印:
Filtering
Incrementing
Doubling
put
4
Run Code Online (Sandbox Code Playgroud)
至于我,如果按照我编写操作的顺序从左到右执行它会更具可读性.
我看到了问题的答案:所有用法都forSome可以用等效用法代替_吗?,但不明白什么是"_"不能用来代替"forSome"的真实情况.我在Scala编程书中读到:
存在类型是语言的完全支持部分,但实际上它们主要用于从Scala访问Java类型时.我创建了Scala项目并引用了Java一个:
斯卡拉:
object Main {
def main(args: Array[String]): Unit = {
type Test = java.util.Collection[T] forSome { type T }
val contents: Test = (new Wild).contents
type Test2 = java.util.Collection[_]
val contents2: Test2 = (new Wild).contents
// foo((new Wild).contents2) // won't compile
foo1((new Wild).contents2)
foo1((new Wild).contents3)
foo2((new Wild).contents3)
}
def foo(xs: java.util.Map[T, T] forSome { type T }) {}
def foo1(xs: java.util.Map[_, _]) {}
def foo2(xs: java.util.Map[_, _ <: java.lang.Number]) {} …Run Code Online (Sandbox Code Playgroud) scala ×2