Wie funktioniert das NotNull-Merkmal in 2.8 und wird es tatsächlich von jemandem verwendet?
trait NotNull {}
Ich habe versucht zu sehen, wie dieses Merkmal garantieren kann, dass etwas nicht null ist, und ich kann es nicht herausfinden:
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 (*)
Und:
val i = new Object with NotNull //compile-error illegal inheritance
Es gibt offensichtlich eine spezielle Compiler-Behandlung, weil dies kompiliert:
trait MyTrait {}
def main(args: Array[String]) {
val i: MyTrait = null
println(i)
}
Dies bedeutet jedoch nicht,
def main(args: Array[String]) {
val i: NotNull = null //compile error: found Null(null) required NotNull
println(i)
}
BEARBEITEN: Darüber gibt es nichts, was ich in der Programmierung in Scala finden kann