E.Kisaragi
06/07/2020, 3:58 PMFoo!!
mean??fatih
06/07/2020, 4:01 PM!!
?henrikhorbovyi
06/07/2020, 4:05 PMFoo
must not be nulljbnizet
06/07/2020, 4:44 PME.Kisaragi
06/08/2020, 10:31 AMdiesieben07
06/08/2020, 10:34 AME.Kisaragi
06/08/2020, 11:31 AMdiesieben07
06/08/2020, 12:28 PMdmitriy.novozhilov
06/09/2020, 7:40 AMT!!
is a "definitely not null type for type parameter T" used in compiler
When you have some type parameter T
without any upper bounds it can be nullable for some substitutions of T
, so you can not call something like this:
fun Any.foo() {}
fun <T> test(x: T) {
// x has type `T`
x.foo() // error since T may be null
}
But if compiler knows that value of type T
somehow can not be null (e.g. after smartcast) than it should describe it, but it can't remove ?
from type, because T
already don't have T
. For such cases we have T!!
which means intersection type T & Any
fun Any.foo() {}
fun <T> test(x: T) {
if (x != null) {
// x smartcasted to type `T!!`
x.foo() // ok
}
}
E.Kisaragi
06/09/2020, 7:56 AMdmitriy.novozhilov
06/09/2020, 7:59 AMT!!
introduced as separate kind of type because it's more easy and performant to use it instead of intersection type in such specific caseE.Kisaragi
06/09/2020, 8:01 AM