I’ll lead with the code:
class MyOption<V: Any>(private var value: V?) {
fun hasValue(): Boolean {
return value == null
}
fun clear() {
value = null
}
fun set(value: V) {
this.value = value
}
fun get(): V {
return value!!
}
}
class SomeContainer<T: Any?>(private val param: T) {
fun hasValue(): MyOption<T & Any> {
// return MyOption<T & Any>(param); // DOESN'T Work, compiler complains
// works fine
return if (param == null) {
MyOption(null)
} else {
MyOption(param)
}
}
}
Can someone help me understand why the code in SomeContainer<T>::hasValue
only works with the “if” checking for null?