what does `Foo!!` mean??
# announcements
e
what does
Foo!!
mean??
f
You mean
!!
?
h
You are telling to the compiler that
Foo
must not be null
j
e
i meant type
d
There is no
Foo!!
type. You probably mean
Foo!
, which is a platform type.
e
d
I don't think that is actualyl implemented, but don't quote me on that. Would be useful!
d
T!!
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:
Copy code
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
Copy code
fun Any.foo() {}

fun <T> test(x: T) {
    if (x != null) {
        // x smartcasted to type `T!!`
        x.foo() // ok
    }
}
e
So does 'T!!' stands for 'T & Any' ?
d
Yes.
T!!
introduced as separate kind of type because it's more easy and performant to use it instead of intersection type in such specific case
e
thanks, your help was solved one of stucked my question 😄
👌 1