Sam Stone
05/13/2024, 7:45 PMfun <T> T?.foo(): T
and
fun <T: Any> T?.foo(): T
? Does one or both guarantee that the output will be non-null?Michael Krussel
05/13/2024, 7:50 PMT can be a nullable type and the compile will force you to handle the possible null return from foo.
I think the default type inference will be the non null, but you can force it.
null.foo<Any?>() will have a return type of Any?Sam
05/13/2024, 7:50 PMSam Stone
05/13/2024, 8:06 PMMichael Krussel
05/13/2024, 8:07 PMnullable with the second version, but T will not be so the return type will not be.Sam
05/13/2024, 8:09 PMT?, so no, it's not the same as T. The added ? means it can always be null, even if T itself is a non-null type.ephemient
05/13/2024, 8:10 PMval x: String?
x.foo<String>()
x.foo<String?>()
are possible. with the second, only
x.foo<String>()
is possible. (well plus supertypes, so x.foo<Any>() etc. may also be possible)Stephan Schröder
05/14/2024, 8:06 AMfun <T> T?.foo(): T & Any
(see definitely-non-nullable-types)