voohbar
07/26/2023, 5:50 PMT
generic argument it implicitely uses Any?
as the upper boundary. Otherwise what would T : Any
do? But then it is not letting me to set T
to null
.
interface Form<T> {
}
abstract class SpecificForm<T> {
private var bean: T = null // does not compile
}
voohbar
07/26/2023, 5:51 PMbean
as T?
when T
already is includes nullable types?Jeff Lockhart
07/26/2023, 5:51 PMT
could be a non-null type. It's not guaranteed to be nullable.voohbar
07/26/2023, 5:54 PMinterface Form<T> {
fun getBean(): T
}
class GenericForm<T : Any?> : Form<T> {
override fun getBean(): T {
return null
}
}
why can’t i return null
here? the T is clearly declared as nullablevoohbar
07/26/2023, 5:55 PMgetBean()
to return T?
then non of the interface implementations can return non-nullable T
😞Joffrey
07/26/2023, 5:55 PM: Any?
doesnt add any bounds to T
Joffrey
07/26/2023, 5:56 PMT
stillvoohbar
07/26/2023, 5:57 PMGenericForm
as having String
and then getBean
would return null
which does not conform to T
Jeff Lockhart
07/26/2023, 5:59 PMmy problem is that if I declareSubclasses can override a function with a nullable return type to narrow the scope to return a non-null.to returngetBean()
then non of the interface implementations can return non-nullableT?
T
voohbar
07/26/2023, 5:59 PMvoohbar
07/26/2023, 6:00 PMfun setBean(bean: T?)
cannot be overridden as fun setBean(bean: T)
Jeff Lockhart
07/26/2023, 6:02 PMvoohbar
07/26/2023, 6:06 PM