!!
wird zur Laufzeit ausgewertet, es ist nur ein Operator.
Der Ausdruck (x!!)
- wirft ein
KotlinNullPointerException
wenn x == null
,
- Andernfalls es
x
Guss zu dem entsprechenden nicht-NULL festlegbaren Typ zurückgibt (beispielsweise es gibt ihn als String
wenn sie auf einer Variable namens mit Typ String?
).
Dies macht natürlich null!!
Kurzschrift für throw KotlinNullPointerException()
.
Wenn es hilft, kann man sich vorstellen !!
als das gleiche zu tun wie eine Funktion wie folgt funktioniert:
fun <T> T?.toNonNullable() : T {
if(this == null) {
throw KotlinNullPointerException()
}
return this as T // this would actually get smart cast, but this
// explicit cast demonstrates the point better
}
So x!!
tun Sie würde das gleiche Ergebnis wie x.toNonNullable()
.
Und deine Frage ist? – tyczj
Sollte es nicht einen Kompilierzeitfehler werfen? – RBK
FYI: IntelliJ IDEA markiert 'val myvar: String =' mit einer Warnung: "Unreachable code". – mfulton26