我可以在Predef的API文档中看到它们是泛型函数类型(From)=> To的子类,但就是这样.嗯什么?也许某处有文档,但搜索引擎不能很好地处理"<:<"之类的"名称",所以我无法找到它.
后续问题:我什么时候应该使用这些时髦的符号/类,为什么?
trait NotNull {}
Run Code Online (Sandbox Code Playgroud)
我一直试图看看这个特性如何保证某些东西不是空的我无法弄清楚:
def main(args: Array[String]) {
val i = List(1, 2)
foo(i) //(*)
}
def foo(a: Any) = println(a.hashCode)
def foo(@NotNull a: Any) = println(a.hashCode) //compile error: trait NotNull is abstract
def foo(a: Any with NotNull) = println(a.hashCode) //compile error: type mismatch at (*)
Run Code Online (Sandbox Code Playgroud)
和:
val i = new Object with NotNull //compile-error illegal inheritance
Run Code Online (Sandbox Code Playgroud)
显然有一些特殊的编译器处理正在进行,因为它编译:
trait MyTrait {}
def main(args: Array[String]) {
val i: MyTrait = null
println(i)
}
Run Code Online (Sandbox Code Playgroud)
然而,这不是:
def main(args: Array[String]) {
val i: NotNull …Run Code Online (Sandbox Code Playgroud)