Null is compatible across different Types:
fun foo(i: Int?) {
if (i == null) {
val s: String? = i
}
}
I want to recreate this behavior but am unsure how. The following is invalid and I’m not sure why as it seems sound as far as I can tell. As Nothing can never exist this would always be valid.
I could change it to Nullable<out T>
but I can’t see why that’s necessary, and the ability to have in parameters of T
is lost.
sealed class Nullable<T>
class Actual<T>(val it: T) : Nullable<T>()
object Null : Nullable<Nothing>()
fun bar(): Nullable<Int> {
return Null // Type mismatch
}